Skip to content

Commit 4b7d6fe

Browse files
committed
fix(plugins): preserve ordinary stop handling
1 parent 8d2ea0c commit 4b7d6fe

2 files changed

Lines changed: 40 additions & 17 deletions

File tree

‎src/plugins/services.test.ts‎

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ describe("startPluginServices", () => {
171171
expectServiceLifecycleState({ starts, stops, contexts, config });
172172
});
173173

174-
it("drains diagnostics after producers stop and before exporters detach", async () => {
174+
it("drains producer diagnostics before exporters stop and propagates exporter failures", async () => {
175175
const order: string[] = [];
176+
const producerError = new Error("producer stop failed");
177+
const exporterError = new Error("exporter stop failed");
176178
let unsubscribe: () => void = () => undefined;
177179
const registry = createRegistry(
178180
[
@@ -186,13 +188,27 @@ describe("startPluginServices", () => {
186188
level: "INFO",
187189
message: "queued during producer shutdown",
188190
});
191+
throw producerError;
189192
},
190193
},
191194
],
192195
"plugin:test",
193196
"workspace",
194197
);
195198
registry.services.push(
199+
...createRegistry(
200+
[
201+
{
202+
id: "diagnostics-prometheus",
203+
start: () => undefined,
204+
stop: () => {
205+
order.push("prometheus");
206+
},
207+
},
208+
],
209+
"diagnostics-prometheus",
210+
"bundled",
211+
).services,
196212
...createRegistry(
197213
[
198214
{
@@ -205,8 +221,9 @@ describe("startPluginServices", () => {
205221
});
206222
},
207223
stop: () => {
208-
order.push("exporter");
224+
order.push("otel");
209225
unsubscribe();
226+
throw exporterError;
210227
},
211228
},
212229
],
@@ -219,10 +236,15 @@ describe("startPluginServices", () => {
219236
registry,
220237
config: createServiceConfig(),
221238
});
222-
await handle.stop();
239+
240+
await expect(handle.stop()).rejects.toBe(exporterError);
223241
await waitForDiagnosticEventsDrained();
224242

225-
expect(order).toEqual(["producer", "event", "exporter"]);
243+
expect(order).toEqual(["producer", "event", "otel", "prometheus"]);
244+
expect(mockedLogger.warn.mock.calls).toEqual([
245+
["plugin service stop failed (producer): Error: producer stop failed"],
246+
["plugin service stop failed (diagnostics-otel): Error: exporter stop failed"],
247+
]);
226248
});
227249

228250
it("rolls back partially started services before starting their siblings", async () => {
@@ -534,7 +556,7 @@ describe("startPluginServices", () => {
534556
await handle.stop();
535557
});
536558

537-
it("attempts every service stop and reports independent failures", async () => {
559+
it("attempts every ordinary service stop and preserves warn-and-continue failures", async () => {
538560
const stopOk = vi.fn();
539561
const firstError = new Error("first stop failed");
540562
const secondError = new Error("second stop failed");
@@ -557,7 +579,7 @@ describe("startPluginServices", () => {
557579
],
558580
});
559581

560-
const stopError = await handle.stop().catch((error: unknown) => error);
582+
await expect(handle.stop()).resolves.toBeUndefined();
561583

562584
expect(mockedLogger.error.mock.calls).toEqual([
563585
[
@@ -572,10 +594,6 @@ describe("startPluginServices", () => {
572594
expect(stopOk).toHaveBeenCalledOnce();
573595
expect(stopFirst).toHaveBeenCalledOnce();
574596
expect(stopSecond).toHaveBeenCalledOnce();
575-
expect(stopError).toBeInstanceOf(AggregateError);
576-
expect(stopError).toMatchObject({
577-
errors: [secondError, firstError],
578-
});
579597
});
580598

581599
it("continues starting siblings when rollback also fails", async () => {

‎src/plugins/services.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ export async function startPluginServices(params: {
241241
(stopPromise ??= Promise.resolve().then(async () => {
242242
const reversed = running.toReversed();
243243
const diagnosticsExporters = reversed.filter((entry) => entry.diagnosticsExporter);
244-
const failures: unknown[] = [];
245-
const stopServices = async (services: typeof reversed) => {
244+
const exporterFailures: unknown[] = [];
245+
const stopServices = async (services: typeof reversed, failures?: unknown[]) => {
246246
for (const entry of services) {
247247
await stopService(entry, failures);
248248
}
@@ -252,12 +252,17 @@ export async function startPluginServices(params: {
252252
// Producers stop first; this barrier preserves their queued tail before exporters detach.
253253
await waitForDiagnosticEventsDrained();
254254
}
255-
await stopServices(diagnosticsExporters);
256-
if (failures.length === 1) {
257-
throw failures[0];
255+
// Ordinary plugin cleanup stays warn-and-continue. Trusted diagnostics
256+
// exporter failures propagate because they can mean telemetry was lost.
257+
await stopServices(diagnosticsExporters, exporterFailures);
258+
if (exporterFailures.length === 1) {
259+
throw exporterFailures[0];
258260
}
259-
if (failures.length > 1) {
260-
throw new AggregateError(failures, "multiple plugin services failed to stop");
261+
if (exporterFailures.length > 1) {
262+
throw new AggregateError(
263+
exporterFailures,
264+
"multiple diagnostics exporters failed to stop",
265+
);
261266
}
262267
})),
263268
};

0 commit comments

Comments
 (0)