FunctionReference destructor called on wrong thread
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- cpp, javascript
調査の方向性
Start with TimeoutDispatcher::CallFunction and the FunctionReference ownership described in the issue, then trace how TimeoutDispatcher::ThreadedFunction releases the last reference. Exercise the setTimeout and Window polyfill path with several closely spaced events on macOS. Done means FunctionReference destruction and napi_delete_reference consistently occur on the JS runtime thread without the CHECK_ENV failure.
索引モデルが issue の本文から書いたものです。
説明
Describe the bug
I'm occasionally seeing shared_ptr ref counts to FunctionReference's going to zero inside the TimeoutDispatcher's ThreadedFunction. This triggers the destructor for the FunctionReference which is not on the JS runtime thread. This can cause the CHECK_ENV assert to fail when calling napi_delete_reference.
To Reproduce
I think this specifically happens with functions given to setTimeout when using the Window polyfill. I haven't been able to repro it with a minimal example, but it seems to happen when I have a bunch of setTimeout events that go off near each other. e.g., If I have a bunch of dispose() calls on scene objects in my render loop for several frames in a row.
Expected behavior
It looks like most of the time, the ref count to the shared pointer goes to zero inside the thread managing the js runtime, so the call to napi_delete_reference runs correctly.
Screenshots
Here's a sample stack trace where the assert fails.
Other
- Platform: macOS
I played around with some modifications and got something to work a little more reliably. In particular, I think the std::shared_ptr is very confusing to debug when this particular object has to be destructed in a very particular location. I think a std::unique_ptr makes it much more clear how to ensure the object is destructed on the correct thread. The only reason a unique_ptr wasn't used I believe is because the dispatcher needs a std::function with a lambda capturing the smart pointer. Since std::function is copyable, it can't have a lambda that has move-captured a unique_ptr.
I instead came up with this:
void TimeoutDispatcher::CallFunction(std::unique_ptr<Napi::FunctionReference> function)
{
if (function)
{
// Since we need a std::function and a std::function is copyable, we can't
// just move-capture the unique_ptr when forming the lambda. Instead, release
// the raw pointer, capture it in the lambda, and then make sure to delete it
// manually when we're done.
auto ptr = function.release();
auto fn = [ptr](Napi::Env env) {
try {
ptr->Call({});
delete ptr;
} catch (...) {
delete ptr;
throw;
}
};
m_runtime.Dispatch(std::move(fn));
}
}
I also double checked the ref count on the version with the shared_ptr and it always appears to be 1 once it reaches CallFunction, even in the event that the assert is failed.
- 主要言語
- C++
- スター
- 22
- フォーク
- 23
- 平均マージ
- 2時間 29分
- マージ済み PR(30日)
- 3
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
BabylonJS/JsRuntimeHost のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
BabylonJS/JsRuntimeHost#234 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
BabylonJS/JsRuntimeHost#173 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 42/100
BabylonJS/JsRuntimeHost#241 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
BabylonJS/JsRuntimeHost#228 ·
メンテナーはふだん 1 日以内に返信
-
napi_unwrap does not reject objects that were never wrapped (V8 port faults, QuickJS port confuses types)対応中かも @bghgary が 51 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 68/100
BabylonJS/JsRuntimeHost#226 ·
メンテナーはふだん 1 日以内に返信
BabylonJS/JsRuntimeHost の issue をすべて見る
似ている issue
-
The Outdated Version prompt shows the release tag as unescaped HTML対応中かも @on8st が今日担当しました。 オープンbug good first issue GUI maintainer-review priority: low
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
aethersdr/AetherSDR#6405 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
bitcoin-core/gui-qml#1003 ·
メンテナーはふだん 3 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 63/100
metasequoiaime/msime#7328 ·
メンテナーはふだん 1 日以内に返信
-
[Linux] --calibrate combined with --model silently falls through to plain re-setup (setup.py:4958)オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信