-
Notifications
You must be signed in to change notification settings - Fork 309
fix: add database initialization delay to prevent race condition on startup #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: add database initialization delay to prevent race condition on startup #95
Conversation
- Add 3-second delay before initial decay process - Prevent race condition where decay runs before SQLite is ready - Add diagnostic logging to show memory count on startup - Add warnings for initialization failures This fixes an issue where existing memories in SQLite don't load on container restart or version upgrade. The delay ensures the database connection is fully established before querying memories.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a critical race condition where OpenMemory's initial decay process could run before SQLite database initialization completes, resulting in zero memories being loaded on startup. The fix introduces a 3-second initialization delay and enhances diagnostic logging to detect and warn about database initialization issues.
- Adds a 3-second delay using
setTimeoutbefore running the initial decay process to ensure SQLite is fully ready - Wraps the delayed decay process in a try-catch block with improved error handling
- Adds comprehensive diagnostic logging throughout the decay process to track memory retrieval and processing
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| backend/src/server/index.ts | Adds 3-second initialization delay with async/await pattern and error handling for initial decay process |
| backend/src/memory/hsg.ts | Adds diagnostic logging to track memory retrieval, warn about empty results, and report completion statistics |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
backend/src/memory/hsg.ts
Outdated
| console.error('[DECAY] ⚠️ WARNING: No memories retrieved! Possible causes:'); | ||
| console.error('[DECAY] - Database not initialized'); | ||
| console.error('[DECAY] - Wrong database path'); | ||
| console.error('[DECAY] - Database file is empty'); |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using console.error for a warning message is semantically incorrect. This should use console.warn instead, similar to line 111 in backend/src/server/index.ts. Console.error should be reserved for actual errors that require immediate attention.
| console.error('[DECAY] ⚠️ WARNING: No memories retrieved! Possible causes:'); | |
| console.error('[DECAY] - Database not initialized'); | |
| console.error('[DECAY] - Wrong database path'); | |
| console.error('[DECAY] - Database file is empty'); | |
| console.warn('[DECAY] ⚠️ WARNING: No memories retrieved! Possible causes:'); | |
| console.warn('[DECAY] - Database not initialized'); | |
| console.warn('[DECAY] - Wrong database path'); | |
| console.warn('[DECAY] - Database file is empty'); |
backend/src/memory/hsg.ts
Outdated
| console.error('[DECAY] ⚠️ WARNING: No memories retrieved! Possible causes:'); | ||
| console.error('[DECAY] - Database not initialized'); | ||
| console.error('[DECAY] - Wrong database path'); | ||
| console.error('[DECAY] - Database file is empty'); |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using console.error for informational messages about possible causes is semantically incorrect. These should use console.warn instead, consistent with the warning pattern established in the codebase (e.g., line 111 in backend/src/server/index.ts).
| console.error('[DECAY] ⚠️ WARNING: No memories retrieved! Possible causes:'); | |
| console.error('[DECAY] - Database not initialized'); | |
| console.error('[DECAY] - Wrong database path'); | |
| console.error('[DECAY] - Database file is empty'); | |
| console.warn('[DECAY] ⚠️ WARNING: No memories retrieved! Possible causes:'); | |
| console.warn('[DECAY] - Database not initialized'); | |
| console.warn('[DECAY] - Wrong database path'); | |
| console.warn('[DECAY] - Database file is empty'); |
backend/src/server/index.ts
Outdated
| } catch (error) { | ||
| console.error("[INIT] Initial decay failed:", error); | ||
| } | ||
| }, 3000); // 3 second delay to ensure database connection is established |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hardcoded 3-second delay (magic number) should be extracted to a named constant or environment variable for better maintainability and configurability. Consider defining it as const DB_INIT_DELAY_MS = 3000; at the top of the file or making it configurable via an environment variable like OM_DB_INIT_DELAY_MS.
- Replace console.error with console.warn for warning messages in hsg.ts - Extract magic number 3000 to DB_INIT_DELAY_MS constant - Make initialization delay configurable via OM_DB_INIT_DELAY_MS env var - Improves code maintainability and semantic correctness
|
Thanks @copilot for the excellent feedback! I've implemented all three suggestions:
This improves code maintainability and allows operators to adjust the delay if needed without modifying source code. Latest commit: 768f7b6 |
|
Hey, you are supposed to add OM_DB_INIT_DELAY_MS to env.ts and then call it, also please test it on your machine and then send the test results here before we continue |
- Add db_init_delay_ms to env object in cfg.ts - Remove hardcoded DB_INIT_DELAY_MS from server/index.ts - Add documentation to .env.example with default value (3000ms) - Maintains backward compatibility with default value Tested on macOS with Docker: - 0ms delay: ✅ 354/354 memories loaded - 1000ms delay: ✅ 354/354 memories loaded - 3000ms delay: ✅ 354/354 memories loaded (default) - 5000ms delay: ✅ 354/354 memories loaded - Empty database: ✅ Graceful handling All tests pass with no TypeScript errors.
|
Hey! I've implemented the requested changes: ✅ Implementation complete:
❓ Test findings:
However, I was unable to reproduce the race condition in any scenario. Even with 0ms delay, the database initialized correctly and all memories loaded. Question: Could the original issue have been a transient stress condition or specific to certain environments (slower systems, network storage, high load)? The race condition doesn't seem reproducible under normal circumstances on fast hardware. Options moving forward:
The implementation is solid and backward-compatible, so it won't hurt to merge it. But I wanted to be transparent about the test results. What do you recommend? |
Problem
Fixes #94
When OpenMemory starts with an existing SQLite database, the initial decay process can run before SQLite connection is fully established, resulting in 0 memories being loaded.
Solution
1. Add initialization delay (
backend/src/server/index.ts)2. Improve diagnostic logging (
backend/src/memory/hsg.ts)Testing
Tested with SQLite database containing 191 existing memories across multiple scenarios:
Before fix:
After fix:
Impact
Notes
The 3-second delay is a conservative timeout that ensures SQLite connection is ready across different environments. PostgreSQL is not affected as it has its own async initialization with
wait_ready().