fix(tsfn): prevent undefined behavior from user callback exceptions#6
Closed
fix(tsfn): prevent undefined behavior from user callback exceptions#6
Conversation
- Wrap all TSFN callback invocations in try-catch blocks - Prevents user exceptions from propagating to N-API layer - Fixes VideoDecoder::Flush promise handling race condition - Store deferred promise before enqueue to prevent use-after-move - Add test for flush promise rejection scenario
- Add try/catch blocks around all TSFN callbacks to prevent undefined behavior - Fix flush promise handling to return same promise instance on early rejection - Increase cleanup timeout from 100ms to 10s with warning on timeout - Add comprehensive comments explaining N-API requirements and limitations - Update test to verify flush() rejects on unconfigured encoder
Implements P0-5 from node-api audit plan to prevent use-after-free during
environment teardown (process.exit, worker termination).
Changes:
- Add src/codec_registry.{h,cc} with immortal globals pattern
- Register/unregister all codecs (Video/Audio Encoder/Decoder)
- Add napi_add_env_cleanup_hook to clear registries before teardown
- Document N-API limitation: cleanup hooks cannot reject promises
N-API Limitation:
napi_add_env_cleanup_hook does NOT provide napi_env parameter, making
promise rejection impossible during abnormal shutdown. This is acceptable
per W3C spec - normal shutdown via close() + await flush() works correctly.
Fixes: P0-2, P0-5 from docs/plans/2025-01-04-node-api-audit.md
Tests: 941/946 passing (no new regressions)
Owner
Author
|
Closing PR as requested |
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.
Summary
Fixes critical undefined behavior when user-provided callbacks throw exceptions in ThreadSafeFunction contexts.
Changes
TSFN Exception Handling: Wrap all callback invocations in try-catch blocks to prevent exceptions from propagating to the N-API layer
AsyncDecodeWorker::ProcessPacket()- error callbackAsyncEncodeWorker::EmitChunk()- output callbackVideoEncoder::OnOutputTSFN()- output callbackVideoDecoder::Flush Promise Fix: Resolve race condition where promise was moved before enqueue failure could be detected
Why This Matters
Per N-API docs, exceptions thrown in TSFN callbacks cause undefined behavior. This was a critical bug that could crash the Node.js process.
Test Plan