Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(sdk): release stopped provider after shutdown rejection (#840)

Rudy Mizrahi Celekli8579f44bf84a
diff --git a/docs/sdk/lifecycle.mdx b/docs/sdk/lifecycle.mdx
index 71f587cd..067de831 100644
--- a/docs/sdk/lifecycle.mdx
+++ b/docs/sdk/lifecycle.mdx
@@ -28,7 +28,7 @@ Flushing inside every web request can add latency. In long-running services, let
## Shutdown
-`Observe.shutdown()` disables instrumentations, shuts down the provider, and clears SDK state so the process can initialize again with different options.
+`Observe.shutdown()` disables instrumentations, shuts down the provider, and clears SDK state so the process can initialize again with different options. Exporter shutdown failures are still reported to the caller, but the stopped provider is released so a later initialization can recover.
```typescript title="Shut down tracing" lines
await Observe.shutdown();
diff --git a/packages/asymptote-sdk-js/src/index.ts b/packages/asymptote-sdk-js/src/index.ts
index 917fcc30..e1ab2f56 100644
--- a/packages/asymptote-sdk-js/src/index.ts
+++ b/packages/asymptote-sdk-js/src/index.ts
@@ -387,9 +387,15 @@ export async function flush(): Promise<void> {
}
export async function shutdown(): Promise<void> {
- state?.instrumentations.forEach(instrumentation => instrumentation.disable());
- await state?.provider.shutdown();
- state = undefined;
+ const current = state;
+ if (!current) return;
+ current.instrumentations.forEach(instrumentation => instrumentation.disable());
+ try {
+ await current.provider.shutdown();
+ } finally {
+ // A stopped provider cannot resume exporting, even when its exporter rejects.
+ if (state === current) state = undefined;
+ }
}
export function isInitialized(): boolean {
diff --git a/packages/asymptote-sdk-js/src/shutdown-recovery.test.ts b/packages/asymptote-sdk-js/src/shutdown-recovery.test.ts
new file mode 100644
index 00000000..2dfdad73
--- /dev/null
+++ b/packages/asymptote-sdk-js/src/shutdown-recovery.test.ts
@@ -0,0 +1,30 @@
+import { InMemorySpanExporter } from "@opentelemetry/sdk-trace-base";
+import { expect, it } from "vitest";
+import { flush, initialize, isInitialized, observe, shutdown } from "./index.js";
+
+class RejectingShutdownExporter extends InMemorySpanExporter {
+ override async shutdown(): Promise<void> {
+ throw new Error("synthetic exporter shutdown failure");
+ }
+}
+
+it("clears a stopped provider after reporting exporter shutdown failure", async () => {
+ const first = new RejectingShutdownExporter();
+ initialize({ serviceName: "first-service", spanExporter: first, disableBatch: true, disableInstrumentations: true });
+ observe({ name: "first-control" }, () => "first")();
+ await flush();
+ expect(first.getFinishedSpans()).toHaveLength(1);
+ await expect(shutdown()).rejects.toThrow("synthetic exporter shutdown failure");
+ expect(isInitialized()).toBe(false);
+
+ const replacement = new InMemorySpanExporter();
+ initialize({ serviceName: "replacement-service", spanExporter: replacement, disableBatch: true, disableInstrumentations: true });
+ try {
+ observe({ name: "replacement-control" }, () => "replacement")();
+ await flush();
+ expect(replacement.getFinishedSpans()).toHaveLength(1);
+ expect(replacement.getFinishedSpans()[0].resource.attributes["service.name"]).toBe("replacement-service");
+ } finally {
+ await shutdown();
+ }
+});