Make dev share acquisition transactional

This commit is contained in:
Theo Browne committed 2026-07-26 20:40:30 -07:00
1 parent e125c343c0
commit df39785c51
2 files changed
+12 -7

No files matched your search

+5 -2
View File
@@ -191,13 +191,14 @@ describe("shareDevServer", () => {
});
it.layer(NodeServices.layer)("dev share cleanup ownership", (it) => {
it.effect("keeps the prior owner when a replacement share fails", () =>
it.effect("cleans up a claimed mapping when replacement sharing fails", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const directory = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-dev-share-lease-",
});
const leasePath = `${directory}/5788.owner`;
const calls: Array<ReadonlyArray<string>> = [];
yield* fileSystem.writeFileString(leasePath, "old-runner");
yield* acquireDevShare({
@@ -207,6 +208,7 @@ it.layer(NodeServices.layer)("dev share cleanup ownership", (it) => {
}).pipe(
Effect.provide(
spawnerLayer({
calls,
off: { exitCode: 0 },
serve: { exitCode: 1, stderr: "permission denied" },
}),
@@ -214,7 +216,8 @@ it.layer(NodeServices.layer)("dev share cleanup ownership", (it) => {
Effect.flip,
);
assert.equal(yield* fileSystem.readFileString(leasePath), "old-runner");
assert.equal(yield* fileSystem.readFileString(leasePath), "new-runner");
assert.equal(calls.filter((args) => args.includes("off")).length, 2);
}),
);
+7 -5
View File
@@ -309,14 +309,16 @@ export const shareDevServer = Effect.fn("devShare.shareDevServer")(function* (in
});
/**
* Claims cleanup ownership only after the tailnet mapping exists. A failed
* replacement must leave the prior runner as the owner so its finalizer can
* still remove any mapping that survived the attempt.
* Claims cleanup ownership before publishing, then cleans up immediately if
* publishing fails. This keeps the handoff atomic from the runners' point of
* view: the prior runner can stop cleaning as soon as the successor claims,
* because that successor owns both the attempted mapping and its rollback.
*/
export const acquireDevShare = Effect.fn("devShare.acquireDevShare")(function* (
lease: DevShareLease,
) {
const shared = yield* shareDevServer({ webPort: lease.webPort });
yield* claimDevShareLease(lease);
return shared;
return yield* shareDevServer({ webPort: lease.webPort }).pipe(
Effect.tapError(() => cleanupOwnedDevShare(lease)),
);
});