Skip to content

[spark-compete] fix: close SQLite database gracefully on SIGINT/SIGTERM - #839

Open
binance1230 wants to merge 1 commit into
vibeforge1111:mainfrom
binance1230:fix/db-graceful-shutdown
Open

[spark-compete] fix: close SQLite database gracefully on SIGINT/SIGTERM#839
binance1230 wants to merge 1 commit into
vibeforge1111:mainfrom
binance1230:fix/db-graceful-shutdown

Conversation

@binance1230

@binance1230 binance1230 commented Jun 10, 2026

Copy link
Copy Markdown
Contributor
{
  "schema": "spark-compete-hotfix-v1",
  "event": "spark-compete-first-event",
  "submission_mode": "public_repo_pr",
  "submission_target_url": "https://github.com/vibeforge1111/spark-telegram-bot/pull/839",
  "team": {
    "name": "king",
    "members": [
      "@king1005678",
      "@AtwoodJonathan1",
      "@JaredAddison12"
    ],
    "llm_device_holder": "@king1005678",
    "device_holder_github": "binance1230",
    "github_accounts": [
      "binance1230"
    ]
  },
  "target_repo": {
    "id": "vibeforge1111/spark-telegram-bot",
    "source": "https://github.com/vibeforge1111/spark-telegram-bot",
    "owner_surface": "telegram-bot"
  },
  "issue": {
    "type": "bug",
    "severity": "high",
    "title": "SQLite database not closed on SIGINT/SIGTERM \u2014 risks WAL corruption",
    "actual_behavior": "SIGINT/SIGTERM handlers call releaseGatewayOwnership and stop the bot, but do not close the SQLite database. This risks WAL corruption on unclean shutdown.",
    "expected_behavior": "On shutdown signals, the database should be gracefully closed: checkpoint the WAL, then close the connection.",
    "repro_steps": [
      "1. Start the bot and let it open the SQLite database.",
      "2. Send SIGINT or SIGTERM to the process.",
      "3. Observe that the database is not closed \u2014 the WAL file may be left in an inconsistent state."
    ],
    "affected_workflow": "Telegram bot shutdown and data integrity"
  },
  "evidence": {
    "safe_links_only": true,
    "before_after_proof": "Before: SIGINT/SIGTERM handlers do not close SQLite. After: closeJsonState() checkpoints WAL and closes the connection before the process exits.",
    "links": [
      "https://github.com/vibeforge1111/spark-telegram-bot/pull/839"
    ],
    "forbidden": [
      "No raw logs, private conversations, secrets, tokens, screenshots of private chats, or archive downloads."
    ]
  },
  "proposed_fix": {
    "approach": "Add closeJsonState() function that checkpoints WAL (PRAGMA wal_checkpoint(TRUNCATE)) and closes the SQLite connection. Call it from both SIGINT and SIGTERM signal handlers.",
    "files_expected": [
      "src/jsonState.ts",
      "src/index.ts"
    ],
    "tests_or_smoke": "smoke_test: sending SIGINT/SIGTERM triggers closeJsonState() which checkpoints WAL and closes the db"
  },
  "pr": {
    "url": "https://github.com/vibeforge1111/spark-telegram-bot/pull/839",
    "branch": "fix/db-graceful-shutdown",
    "title_prefix": "[spark-compete]",
    "author_github": "binance1230",
    "body_must_include": [
      "packet",
      "team",
      "pr_author",
      "repo",
      "actual_behavior",
      "expected_behavior",
      "repro_steps",
      "before_after_proof",
      "tests_or_smoke",
      "duplicate_notes",
      "risk_notes",
      "review_claim"
    ],
    "replaces_url": "https://github.com/vibeforge1111/spark-telegram-bot/pull/87"
  },
  "review_claim": {
    "impact_claim": "high",
    "evidence_types": [
      "smoke_test",
      "redacted_conversation_excerpt"
    ],
    "duplicate_notes": "Replaces #87 (which contained stacked fixes). This PR addresses only the DB shutdown root cause.",
    "risk_notes": "Low risk: closeJsonState() is best-effort with try/catch/finally. If database is not open, it returns immediately. No behavioral change to normal operation.",
    "review_state_requested": "pr_review"
  }
}

SIGINT/SIGTERM handlers did not close the SQLite database, risking WAL
corruption on unclean shutdown. Add closeJsonState() that checkpoints
the WAL and closes the connection, then call it from both signal
handlers.
@binance1230 binance1230 changed the title fix: close SQLite database gracefully on SIGINT/SIGTERM [spark-compete] fix: close SQLite database gracefully on SIGINT/SIGTERM Jun 10, 2026
NoRegretz pushed a commit to NoRegretz/spark-telegram-bot that referenced this pull request Jul 1, 2026
vibeforge1111#839: add closeJsonState() — checkpoint the WAL (TRUNCATE) and close the
better-sqlite handle, best-effort and idempotent (nulls db, early-returns when
already closed). Call it from the SIGINT and SIGTERM handlers so a shutdown
flushes and releases the DB instead of leaving a -wal/-shm behind.

Per maintainer note, added a small unit test asserting closeJsonState() is
idempotent, nulls the handle, and that state reopens lazily on the next read.

PRs: vibeforge1111#839

Co-authored-by: binance1230 <binance1230@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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