Close Collection before process.exit() to prevent Windows libuv assertion crash#5
Open
HeyItsGilbert wants to merge 3 commits into
Open
Close Collection before process.exit() to prevent Windows libuv assertion crash#5HeyItsGilbert wants to merge 3 commits into
HeyItsGilbert wants to merge 3 commits into
Conversation
* Updated multiple command files to use `closeAndExit` for graceful shutdown. * This change ensures that resources are properly released before exiting, preventing potential assertion failures on Windows. * Affected files include: base.ts, create.ts, delete.ts, diff.ts, export.ts, fmt.ts, graph.ts, import.ts, lint.ts, query.ts, read.ts, rename.ts, schema.ts, stats.ts, types.ts, update.ts, validate.ts, watch.ts.
HeyItsGilbert
force-pushed
the
winFileLock
branch
from
March 1, 2026 02:08
0d4359d to
d4adfed
Compare
* Updated `parseFields` function in `create.ts`, `query.ts`, and `update.ts` to use `await closeAndExit` instead of `process.exit`. * This change improves the handling of process termination by allowing for cleanup operations.
HeyItsGilbert
marked this pull request as ready for review
March 1, 2026 15:44
HeyItsGilbert
marked this pull request as draft
March 1, 2026 15:45
HeyItsGilbert
marked this pull request as ready for review
March 1, 2026 15:45
HeyItsGilbert
marked this pull request as draft
March 1, 2026 16:20
- Replace direct `process.exit()` call with a delayed exit to allow libuv close-callbacks to complete. - Ensure `closeAndExit` returns a `Promise<never>` to prevent further execution after calling.
HeyItsGilbert
marked this pull request as ready for review
March 1, 2026 20:20
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Problem
On Windows, running any command that opens a
Collection(e.g.mdbase types create reviews) triggers a libuv assertion failure on exit:This happens because
Collection.open()initializes an in-memory sql.js/WASM SQLite database with prepared statements. Whenprocess.exit()is called without first closing the collection, Node tears down the event loop while these handles are still active. On Linux/macOS this is silently ignored; on Windows, libuv's stricter handle lifecycle checking triggers a fatal assertion.Root Cause
Every command that calls
Collection.open()was callingprocess.exit(N)directly without first callingcollection.close(). Theclose()method flushes the SQLite cache to disk, frees prepared statements, and closes the database — all necessary for a clean shutdown.Only
collections fileshad proper cleanup viatry/finallywithawait collection.close().Solution
Added
closeAndExit()utility in utils.ts — a helper that gracefully closes a Collection (if provided) then callsprocess.exit().Replaced ~70 bare
process.exit()calls across all 16 command files that open a Collection:process.exit()calls before any Collection is opened (e.g. ininit,collections, early argument validation) are left unchanged — they have no handles to clean up.Secondary benefit
Beyond fixing the Windows crash, this also ensures the SQLite cache is properly flushed to disk on every exit path. Previously, cache updates from mutations (
create,update,delete,rename,createType,import,lint --fix,fmt) were silently lost because the in-memory database was never written back.How
closeAndExitworksNote
Tests that spawn the CLI via
execFileSyncmay need timeout adjustments sincecollection.close()adds a small amount of async work before exit. The existing 30stestTimeoutin vitest.config.ts should be sufficient.Fixes #3