A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.
TypeScript
fix(desktop): abandon the stop listener's operation instead of freeing it
Rowell Urbaez Reyese502db001056
diff --git a/packages/daemon/native/windows-stop.cc b/packages/daemon/native/windows-stop.ccindex cad4761..f8adc7f 100644--- a/packages/daemon/native/windows-stop.cc+++ b/packages/daemon/native/windows-stop.cc@@ -169,6 +169,8 @@ Record ReadRecord(const std::wstring& path, PSID sid) {}// Only the native worker uses this heap storage. It is never freed/reused while// the kernel owns an OVERLAPPED, even after close rejection or env destruction.+// When the worker gives up on a cancellation it abandons this storage instead of+// freeing it, so a retirement later than the deadline always has valid memory.struct Operation {Handle event{CreateEventW(nullptr, TRUE, FALSE, nullptr)};OVERLAPPED overlapped{};@@ -193,6 +195,7 @@ struct State {bool participant = false; // gate protects every native TSFN call/releasebool authenticated = false; // worker only: one stop for this launchbool deadlineQueued = false; // worker only+ bool abandoned = false; // worker only: the kernel may still own the operationbool finalized = false; // JS onlystd::shared_ptr<State>* hookOwner = nullptr; // JS only, independently owns hook datastd::atomic<bool> stopping{false}, teardown{false}, deadlineExpired{false}, nativeComplete{false};@@ -247,7 +250,9 @@ bool ExpectedDisconnect(DWORD code) {}// No blocking GetOverlappedResult and no JS-thread join. Once cancellation is// requested, completion alone permits storage/handle release. If completion is-// late, report failure and keep the worker + pipe + heap operation alive.+// late, this reports failure and returns while the kernel may still own the+// operation: the caller must then abandon that storage instead of freeing it,+// because Windows does not promise when, or whether, a cancellation retires.bool Complete(State& s, DWORD timeout, DWORD& transferred) {Operation& op = *s.operation;HANDLE events[] = {s.stop.value, op.event.value};@@ -284,8 +289,14 @@ bool Complete(State& s, DWORD timeout, DWORD& transferred) {// Poll only the retained operation's completion, not the already-set stop// event. If the kernel never retires the cancelled operation, spinning here// would hold the pipe handle and this thread forever, so the loop gives up- // one deadline after the timeout is reported.- if (elapsed >= kDeadlineMs * 2) return false;+ // one deadline after the timeout is reported. The operation is still the+ // kernel's at that point, so it is marked abandoned: the caller must leak it+ // instead of freeing it, since a retirement later than this deadline writes+ // into the OVERLAPPED it owns.+ if (elapsed >= kDeadlineMs * 2) {+ s.abandoned = true;+ return false;+ }DWORD completion = WaitForSingleObject(op.event.value, 50);if (completion == WAIT_FAILED) { s.Fail(GetLastError()); Sleep(50); }else if (completion == WAIT_OBJECT_0) Sleep(10);@@ -393,8 +404,18 @@ DWORD WINAPI Listen(void* data) {}}s.Stop();- // Every issued operation has completed before these resources are released.- s.operation.reset();+ // Every issued operation has completed before these resources are released,+ // except when the worker gave up on the cancellation: then the kernel may+ // still retire it into this storage, so the operation is abandoned instead of+ // being freed here or by ~State. Closing the pipe is still correct: a handle+ // closed with I/O pending cancels it and completes into the OVERLAPPED, which+ // stays alive. This is a deliberate one-Operation leak, the heap analogue of+ // keeping the pipe alive.+ if (s.abandoned) {+ static_cast<void>(s.operation.release());+ } else {+ s.operation.reset();+ }// A failed CloseHandle leaves ownership uncertain; never close the value a second time.HANDLE pipe = s.pipe.value;s.pipe.value = INVALID_HANDLE_VALUE;