Title: sqlite: GC of an unreferenced session frees memory SQLite is still using · Issue #65460 · nodejs/node · GitHub
Open Graph Title: sqlite: GC of an unreferenced session frees memory SQLite is still using · Issue #65460 · nodejs/node
X Title: sqlite: GC of an unreferenced session frees memory SQLite is still using · Issue #65460 · nodejs/node
Description: Version main (21f0f27) Platform Darwin 25.6.0 Darwin Kernel Version 25.6.0: Fri Jul 31 19:19:08 PDT 2026; root:xnu-12377.161.14~5/RELEASE_ARM64_T6050 arm64 Subsystem sqlite What steps will reproduce the bug? Let a session become unreacha...
Open Graph Description: Version main (21f0f27) Platform Darwin 25.6.0 Darwin Kernel Version 25.6.0: Fri Jul 31 19:19:08 PDT 2026; root:xnu-12377.161.14~5/RELEASE_ARM64_T6050 arm64 Subsystem sqlite What steps will reproduc...
X Description: Version main (21f0f27) Platform Darwin 25.6.0 Darwin Kernel Version 25.6.0: Fri Jul 31 19:19:08 PDT 2026; root:xnu-12377.161.14~5/RELEASE_ARM64_T6050 arm64 Subsystem sqlite What steps will reproduc...
Opengraph URL: https://github.com/nodejs/node/issues/65460
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"sqlite: GC of an unreferenced session frees memory SQLite is still using","articleBody":"### Version\n\n`main` (21f0f2786f0)\n\n### Platform\n\n```text\nDarwin 25.6.0 Darwin Kernel Version 25.6.0: Fri Jul 31 19:19:08 PDT 2026; root:xnu-12377.161.14~5/RELEASE_ARM64_T6050 arm64\n```\n\n### Subsystem\n\nsqlite\n\n### What steps will reproduce the bug?\n\nLet a session become unreachable from JavaScript, then let GC run while SQLite is inside its pre-update hook. Run with `--expose-gc`:\n\n```js\nimport { DatabaseSync, constants } from 'node:sqlite';\n\nconst db = new DatabaseSync(':memory:');\ndb.exec('CREATE TABLE t(a PRIMARY KEY)');\ndb.createSession(); // return value dropped: no JS reference kept\n\nlet once = true;\ndb.setAuthorizer((action, p1) => {\n if (action === constants.SQLITE_PRAGMA && p1 === 'table_xinfo' && once) {\n once = false;\n globalThis.gc();\n globalThis.gc();\n }\n return constants.SQLITE_OK;\n});\n\ndb.exec('INSERT INTO t VALUES (1)');\nconsole.log('survived');\n```\n\nA `'sqlite.db.query'` diagnostics-channel subscriber calling `gc()` in place of the authorizer crashes the same way.\n\n### How often does it reproduce? Is there a required condition?\n\nI get exit 139 on every run.\n\n### What is the expected behavior? Why is that the expected behavior?\n\nThe write should complete and the session should still record the change. A session that JavaScript can no longer reach is still registered on the connection and still accumulating changes, so collecting it is observable in two ways beyond the crash: the changeset silently stops recording, and `sqlite3session_delete()` runs at an arbitrary point.\n\n### What do you see instead?\n\n`EXC_BAD_ACCESS` in `sqlite3StrICmp`, from the `sqlite3_strnicmp(zDb, pSession->zDb, nDb + 1)` comparison in `xPreUpdate` (`deps/sqlite/sqlite3.c:235727`) reading a freed `sqlite3_session`.\n\n### Additional information\n\n`Session` calls `MakeWeak()` (`src/node_sqlite.cc:4271`) and `DatabaseSync::sessions_` holds raw `Session*` (`src/node_sqlite.h:340`), so nothing keeps a session alive once JS drops it. SQLite's pre-update hook walks the connection's session list and runs `PRAGMA table_xinfo` from inside that walk, which reaches JavaScript and gives GC a chance to run while the walk still holds pointers into the session being visited.\n\nThis is the same window as #65428, but a different trigger.\n\nThe obvious fix of making `sessions_` hold `BaseObjectPtr\u003cSession>` does not work on its own: `Session::database_` is already a `BaseObjectPtr\u003cDatabaseSync>`, so that creates a cycle that leaks both objects for the life of the process. Options that seem workable: hold strong refs to the attached sessions only for the duration of SQLite calls that can fire the pre-update hook, or keep sessions strongly held by the database and make the back-reference weak.\n","author":{"url":"https://github.com/TrevorBurnham","@type":"Person","name":"TrevorBurnham"},"datePublished":"2026-08-21T17:01:29Z","url":"https://github.com/nodejs/node/issues/65460"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:79ba557a-1557-a579-7e5d-6274d58606f4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E5BA:C8434:3F03F8:51AA0C:6AED2104 |
| html-safe-nonce | 3ca2ed5a2a92a9729e9178c4d2686e956be88d51a6016b52685bf345d5b257d0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNUJBOkM4NDM0OjNGMDNGODo1MUFBMEM6NkFFRDIxMDQiLCJ2aXNpdG9yX2lkIjoiOTE0NjY3MjAxMDE2NTU1OTU1NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0bf40dd18cad298b1e000f042dc8e722b1c5798718fb12fc5b934c13752c7176 |
| hovercard-subject-tag | issue:5216381799 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/65460/issue_layout |
| twitter:image | https://opengraph.githubassets.com/af2c3e5f7c0a16c5b8644d0ae9f547837caf505fecd8eaaec2bf8de8ff3b8a2d/nodejs/node/issues/65460 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/af2c3e5f7c0a16c5b8644d0ae9f547837caf505fecd8eaaec2bf8de8ff3b8a2d/nodejs/node/issues/65460 |
| og:image:alt | Version main (21f0f27) Platform Darwin 25.6.0 Darwin Kernel Version 25.6.0: Fri Jul 31 19:19:08 PDT 2026; root:xnu-12377.161.14~5/RELEASE_ARM64_T6050 arm64 Subsystem sqlite What steps will reproduc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | TrevorBurnham |
| hostname | github.com |
| expected-hostname | github.com |
| None | e3e0f7f63e50c6fe879198a93a0a023a648ecc0a5e9bc3b5e033ac57172237e6 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 707a24b346ac8a82878b9c084efaa38a997660cb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width