Skip to content

Close Matrix OlmMachine on shutdown to avoid napi panic - #61

Closed
strk-ai-agent wants to merge 1 commit into
ominiverdi:mainfrom
strk-ai-agent:napi-workaround
Closed

Close Matrix OlmMachine on shutdown to avoid napi panic#61
strk-ai-agent wants to merge 1 commit into
ominiverdi:mainfrom
strk-ai-agent:napi-workaround

Conversation

@strk-ai-agent

Copy link
Copy Markdown

The Rust OlmMachine returned by @matrix-org/matrix-sdk-crypto-nodejs leaves background tokio tasks pending. When the Bun process exits, the napi-rs env cleanup hook drops the tokio runtime (RT = None) before those tasks complete, and within_runtime_if_available panics:

panicked at .../napi-2.16.17/src/tokio_runtime.rs:114:47:
called `Option::unwrap()` on a `None` value
fatal runtime error: failed to initiate panic, error 5, aborting

This is the well-known race tracked upstream as napi-rs PR #3252 (still open). Since the prebuilt .node binary embeds napi-2.16.17 and the upstream fix isn't released, a clean shutdown requires cancelling pending tokio work before Bun tears down the napi env.

matrix-bot-sdk@0.8.0 doesn't expose a close hook for OlmMachine, but OlmMachine.close() is already exported by the native binding, so this PR calls it directly from the connector's stop() path before logging "Stopped.".

Verified with a minimal repro: before the fix make start-matrix aborts with a core dump on SIGTERM; after the fix it exits 0. Typecheck, full test suite (348 tests) and connector build check all pass.

Filed on a dedicated napi-workaround branch so the connector's main stays clean while we wait for the upstream napi-rs fix and a corresponding matrix-sdk-crypto-nodejs release.

Assisted-By: MiniMax-M3 via opencode 1.18.7

The Rust OlmMachine returned by matrix-sdk-crypto-nodejs leaves
background tokio tasks pending. When the Bun process exits, the
napi-rs env cleanup hook drops the tokio runtime (RT = None) before
those tasks complete, and within_runtime_if_available panics:

    panicked at .../napi-2.16.17/src/tokio_runtime.rs:114:47:
    called `Option::unwrap()` on a `None` value
    fatal runtime error: failed to initiate panic, error 5, aborting

matrix-bot-sdk does not expose a close hook for OlmMachine, so call
machine.close() directly from the connector's stop() path. Verified
that 'make start-matrix' now exits cleanly instead of aborting with a
core dump.

Assisted-By: MiniMax-M3 via opencode 1.18.7
@ominiverdi

Copy link
Copy Markdown
Owner

Closing under the updated contribution policy. The current submission volume and required verification exceed this project’s review capacity, and pull requests from autonomous or delegated agent accounts are no longer accepted. Valid underlying issues may remain open for independent implementation. Please do not revise, reopen, or replace this pull request.

@ominiverdi ominiverdi closed this Aug 10, 2026
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.

3 participants