Skip to content

Commit b89da52

Browse files
Move removal of job into OnWorkComplete
The theory of the crash, produced during a rubber-ducking session with Claude: * `find_words_with_subsequence_in_range` is called * a job is scheduled and placed in `outstanding_workers` * a call to `set_text_in_range` cancels a pending worker via `cancel_queued_workers` and `worker->Cancel` * the worker is never removed from `outstanding_workers` because that happens in `Execute` — and we cancelled before the job got that far * later, `set_text_in_range` triggers another call to `cancel_queued_workers` * the already-cancelled job from before is still present in the set * we try to call `Cancel` on it again, but the memory has been freed The fix is to take the code that removes a job from `outstanding_workers` and move it from `Execute` to `OnWorkComplete`. The latter is guaranteed to be called, no matter how a job finished. Because this is a theory produced by a man and his hallucinating robot, it's important to back it up with proof. The test added in the previous commit should now pass instead of crashing. So this fix works, even if there are slight flaws in the reasoning above.
1 parent 1f1caff commit b89da52

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

src/bindings/text-buffer-wrapper.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,11 @@ void TextBufferWrapper::find_words_with_subsequence_in_range(const CallbackInfo
623623
}
624624

625625
void OnWorkComplete(Napi::Env env, napi_status status) override {
626+
{
627+
std::lock_guard<std::mutex> guard(text_buffer_wrapper->outstanding_workers_mutex);
628+
text_buffer_wrapper->outstanding_workers.erase(this);
629+
}
630+
626631
if (status == napi_cancelled) {
627632
Callback().Call({env.Null()});
628633
}
@@ -631,11 +636,6 @@ void TextBufferWrapper::find_words_with_subsequence_in_range(const CallbackInfo
631636
}
632637

633638
void Execute() override {
634-
{
635-
std::lock_guard<std::mutex> guard(text_buffer_wrapper->outstanding_workers_mutex);
636-
text_buffer_wrapper->outstanding_workers.erase(this);
637-
}
638-
639639
if (!snapshot) {
640640
return;
641641
}

0 commit comments

Comments
 (0)