Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(sqlite): keep a retained worker owner after a failed worker exit (#3242)

Kaoru0822bdacd4140ef6
diff --git a/src/sqlite/SqliteDatabaseProvider.ts b/src/sqlite/SqliteDatabaseProvider.ts
index 03929b2a7..7572034d8 100644
--- a/src/sqlite/SqliteDatabaseProvider.ts
+++ b/src/sqlite/SqliteDatabaseProvider.ts
@@ -288,7 +288,12 @@ export class SqliteDatabaseProvider implements SqliteDatabasePort {
);
result = ok(createSqliteObservation(validated.data, input.path));
} catch (cause: unknown) {
- if (cause instanceof OwnedCommandFailure)
+ // A failure rethrown after retainCleanup carries no owner of its own;
+ // keep the retained worker instead of discarding it.
+ if (
+ cause instanceof OwnedCommandFailure &&
+ cause.cleanupOwner !== undefined
+ )
processOwner = cause.cleanupOwner;
const failure = sqliteDatabaseFailure(
cause,
diff --git a/tests/boundary/sqlite/sqliteDatabaseCleanup.test.ts b/tests/boundary/sqlite/sqliteDatabaseCleanup.test.ts
index de8e14298..6331650dd 100644
--- a/tests/boundary/sqlite/sqliteDatabaseCleanup.test.ts
+++ b/tests/boundary/sqlite/sqliteDatabaseCleanup.test.ts
@@ -3,7 +3,11 @@ import { expect, it, onTestFinished } from "vitest";
import { SqliteDatabaseProvider } from "../../../src/sqlite/SqliteDatabaseProvider.js";
import { PrivateRuntimeRoot } from "../../../src/process/PrivateRuntimeRoot.js";
import { OwnedCommandFailure } from "../../../src/process/OwnedCommand.js";
-import { ProviderProcessSupervisor } from "../../../src/process/ProviderProcess.js";
+import {
+ ProviderProcessSupervisor,
+ spawnOwnedProviderProcess,
+} from "../../../src/process/ProviderProcess.js";
+import { cleanupOwnedProcessGroup } from "../../../src/process/ProcessOwnership.js";
import {
spawnProviderProcessFixture,
stopProviderProcessFixture,
@@ -273,3 +277,48 @@ it("retains the snapshot until a failed worker cleanup owner can be stopped", as
expect(workerAttempts).toBe(4);
await expect(access(root.path)).rejects.toMatchObject({ code: "ENOENT" });
});
+
+it("keeps a retained worker owner when the worker fails and its cleanup is unresolved", async () => {
+ const workspace = await createTestWorkspace("rea-sqlite-retained-worker-");
+ const database = createSqliteDatabaseFixture(workspace.root);
+ database.close();
+ let mayClean = false;
+ let launches = 0;
+ // runOwnedCommand retains the incomplete cleanup and then rethrows the
+ // worker failure without an owner of its own.
+ const provider = new SqliteDatabaseProvider({}, async (spawn) => {
+ launches += 1;
+ const launched = await spawnOwnedProviderProcess({
+ ...spawn,
+ command: process.execPath,
+ arguments: ["-e", "process.exit(3)"],
+ });
+ const cleanupOwned =
+ launched.cleanup ?? (() => cleanupOwnedProcessGroup(launched.ownership));
+ return {
+ ...launched,
+ cleanup: async () => {
+ const cleaned = await cleanupOwned();
+ if (!mayClean) throw new Error("injected retained cleanup failure");
+ return cleaned;
+ },
+ };
+ });
+ onTestFinished(async () => {
+ mayClean = true;
+ await provider.close();
+ await removeTestWorkspace(workspace.root);
+ });
+ expect(await provider.inspect({ path: database.path })).toMatchObject({
+ ok: false,
+ error: { cleanupIncomplete: true },
+ });
+ // The retained owner blocks a new worker until its cleanup succeeds.
+ expect(await provider.inspect({ path: database.path })).toMatchObject({
+ ok: false,
+ error: { cleanupIncomplete: true },
+ });
+ expect(launches).toBe(1);
+ mayClean = true;
+ await provider.close();
+});