Skip to content

fix: correct better-sqlite3 pragma API usage for busy_timeout#140

Open
rohit-purandare wants to merge 2 commits intomainfrom
fix/database-timeout-pragma-api
Open

fix: correct better-sqlite3 pragma API usage for busy_timeout#140
rohit-purandare wants to merge 2 commits intomainfrom
fix/database-timeout-pragma-api

Conversation

@rohit-purandare
Copy link
Copy Markdown
Owner

Fixed incorrect usage of the pragma() method when reading the busy_timeout value. The old code was passing a boolean second parameter which is not valid in the current better-sqlite3 API, causing "Expected second argument to be an options object" errors in debug logs.

Changes:

  • Updated pragma('busy_timeout', true) to pragma('busy_timeout')
  • Changed property access from result[0].busy_timeout to result[0].timeout (the pragma returns a 'timeout' property, not 'busy_timeout')
  • Added comprehensive test suite for transaction timeout and pragma handling

The fix ensures:

  • No more pragma API errors in debug logs
  • Timeout values are correctly read and restored across transactions
  • Transaction functionality continues to work as expected

Fixes the issue reported in logs:
"Could not set database timeout: Expected second argument to be an options object"

Fixed incorrect usage of the pragma() method when reading the busy_timeout
value. The old code was passing a boolean second parameter which is not
valid in the current better-sqlite3 API, causing "Expected second argument
to be an options object" errors in debug logs.

Changes:
- Updated pragma('busy_timeout', true) to pragma('busy_timeout')
- Changed property access from result[0].busy_timeout to result[0].timeout
  (the pragma returns a 'timeout' property, not 'busy_timeout')
- Added comprehensive test suite for transaction timeout and pragma handling

The fix ensures:
- No more pragma API errors in debug logs
- Timeout values are correctly read and restored across transactions
- Transaction functionality continues to work as expected

Fixes the issue reported in logs:
"Could not set database timeout: Expected second argument to be an options object"
Fixed a critical bug where books with progress >= 95% (completion threshold)
were being skipped by the early optimization even though they hadn't been
marked as complete yet. This caused books at 99%+ progress to never get
marked as finished.

Root Cause:
The early skip optimization (lines 617-647 and 759-788 in sync-manager.js)
would return immediately when progress hadn't changed, without checking if
the book needed completion processing. Books that were already above the
95% threshold but hadn't been marked complete were incorrectly skipped.

Changes:
- Modified early skip logic in both cache paths (identifier and title/author)
- Added completion check: progress >= 95% AND no finished_at timestamp
- Books meeting this criteria are no longer skipped; they proceed through
  normal flow to trigger completion detection
- Books already marked complete (with finished_at) can still be safely skipped
- Books below 95% progress continue to benefit from early skip optimization

Test Coverage:
- Unit tests: completion-early-skip-detection.test.js (10 tests)
  * Validates the exact bug scenario (99.77% unchanged progress)
  * Tests edge cases at completion boundary (94.99%, 95.0%, 95.01%)
  * Verifies behavior with and without finished_at timestamp

- Integration tests: completion-early-skip-integration.test.js (8 tests)
  * Real-world sync scenarios across multiple syncs
  * Multiple books at various completion states
  * Title/author cache path verification
  * Edge cases and race conditions
  * Performance verification with 100 books

The fix ensures:
✅ Books above completion threshold get marked as complete even when progress
   hasn't changed between syncs
✅ Already-completed books (with finished_at) are still optimized with early skip
✅ Books below 95% progress continue to benefit from performance optimization
✅ No performance impact on the optimization for non-completion cases
✅ Both ASIN/ISBN and title/author matching paths are covered

Fixes issue where books showing "Early skip: Progress unchanged via cache (99.8%)"
were never marked as completed despite being above the completion threshold.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant