MCPcopy
hub / github.com/coder/mux / finish

Method finish

src/node/utils/oauthFlowManager.ts:164–202  ·  view source on GitHub ↗

* Finish a flow: resolve the deferred, clear the timeout, close the server, * and remove the entry from the map. * * Idempotent — no-op if the flow was already removed. Mirrors the * `finishDesktopFlow` pattern.

(flowId: string, result: Result<void, string>)

Source from the content-addressed store, hash-verified

162 * `finishDesktopFlow` pattern.
163 */
164 async finish(flowId: string, result: Result<void, string>): Promise<void> {
165 const flow = this.flows.get(flowId);
166 if (!flow) return;
167
168 // Remove from map first to make re-entrant calls no-ops.
169 this.flows.delete(flowId);
170
171 if (flow.timeoutHandle !== null) {
172 clearTimeout(flow.timeoutHandle);
173 }
174
175 // Preserve the final result briefly so late waiters can still retrieve it.
176 //
177 // Old per-service DesktopFlow implementations kept completed flows around for
178 // ~60s (cleanupTimeout) to avoid a race where the OAuth callback finishes
179 // before the frontend begins `waitFor`-ing.
180 this.clearCompleted(flowId);
181 const cleanupTimeout = setTimeout(() => {
182 this.completed.delete(flowId);
183 }, this.completedFlowTtlMs);
184
185 // Don't keep the node process alive just to delete old completed entries.
186 if (typeof cleanupTimeout !== "number") {
187 cleanupTimeout.unref?.();
188 }
189
190 this.completed.set(flowId, { result, cleanupTimeout });
191
192 try {
193 flow.resultDeferred.resolve(result);
194
195 if (flow.server) {
196 // Stop accepting new connections.
197 await closeServer(flow.server);
198 }
199 } catch (error) {
200 log.debug("Failed to close OAuth callback listener:", error);
201 }
202 }
203
204 /**
205 * Shut down all active flows — resolves each with an error.

Callers 4

waitForMethod · 0.95
cancelMethod · 0.95
shutdownAllMethod · 0.95

Calls 7

clearCompletedMethod · 0.95
closeServerFunction · 0.90
setMethod · 0.80
resolveMethod · 0.80
debugMethod · 0.80
getMethod · 0.65
deleteMethod · 0.45

Tested by

no test coverage detected