@@ -17,6 +17,11 @@ vi.mock("../logging/subsystem.js", () => ({
1717} ) ) ;
1818
1919import { STATE_DIR } from "../config/paths.js" ;
20+ import {
21+ emitTrustedDiagnosticEvent ,
22+ resetDiagnosticEventsForTest ,
23+ waitForDiagnosticEventsDrained ,
24+ } from "../infra/diagnostic-events.js" ;
2025import { queuePluginSessionsChanged , subscribePluginSessionsChanged } from "./gateway-events.js" ;
2126import { registerPluginHttpRoute } from "./http-registry.js" ;
2227import { resetPluginRuntimeStateForTest , setActivePluginRegistry } from "./runtime.js" ;
@@ -142,6 +147,7 @@ function createTrackingService(
142147describe ( "startPluginServices" , ( ) => {
143148 beforeEach ( ( ) => {
144149 vi . clearAllMocks ( ) ;
150+ resetDiagnosticEventsForTest ( ) ;
145151 resetPluginRuntimeStateForTest ( ) ;
146152 } ) ;
147153
@@ -165,6 +171,82 @@ describe("startPluginServices", () => {
165171 expectServiceLifecycleState ( { starts, stops, contexts, config } ) ;
166172 } ) ;
167173
174+ it ( "drains producer diagnostics before exporters stop and propagates exporter failures" , async ( ) => {
175+ const order : string [ ] = [ ] ;
176+ const producerError = new Error ( "producer stop failed" ) ;
177+ const exporterError = new Error ( "exporter stop failed" ) ;
178+ let unsubscribe : ( ) => void = ( ) => undefined ;
179+ const registry = createRegistry (
180+ [
181+ {
182+ id : "producer" ,
183+ start : ( ) => undefined ,
184+ stop : ( ) => {
185+ order . push ( "producer" ) ;
186+ emitTrustedDiagnosticEvent ( {
187+ type : "log.record" ,
188+ level : "INFO" ,
189+ message : "queued during producer shutdown" ,
190+ } ) ;
191+ throw producerError ;
192+ } ,
193+ } ,
194+ ] ,
195+ "plugin:test" ,
196+ "workspace" ,
197+ ) ;
198+ 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 ,
212+ ...createRegistry (
213+ [
214+ {
215+ id : "diagnostics-otel" ,
216+ start : ( ctx ) => {
217+ unsubscribe = ctx . internalDiagnostics ! . onEvent ( ( event ) => {
218+ if ( event . type === "log.record" ) {
219+ order . push ( "event" ) ;
220+ }
221+ } ) ;
222+ } ,
223+ stop : ( ) => {
224+ order . push ( "otel" ) ;
225+ unsubscribe ( ) ;
226+ throw exporterError ;
227+ } ,
228+ } ,
229+ ] ,
230+ "diagnostics-otel" ,
231+ "bundled" ,
232+ ) . services ,
233+ ) ;
234+
235+ const handle = await startPluginServices ( {
236+ registry,
237+ config : createServiceConfig ( ) ,
238+ } ) ;
239+
240+ await expect ( handle . stop ( ) ) . rejects . toBe ( exporterError ) ;
241+ await waitForDiagnosticEventsDrained ( ) ;
242+
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+ ] ) ;
248+ } ) ;
249+
168250 it ( "rolls back partially started services before starting their siblings" , async ( ) => {
169251 const acquired = new Set < string > ( ) ;
170252 const received = vi . fn ( ) ;
@@ -474,10 +556,15 @@ describe("startPluginServices", () => {
474556 await handle . stop ( ) ;
475557 } ) ;
476558
477- it ( "logs start/ stop failures and continues " , async ( ) => {
559+ it ( "attempts every ordinary service stop and preserves warn- and-continue failures " , async ( ) => {
478560 const stopOk = vi . fn ( ) ;
479- const stopThrows = vi . fn ( ( ) => {
480- throw new Error ( "stop failed" ) ;
561+ const firstError = new Error ( "first stop failed" ) ;
562+ const secondError = new Error ( "second stop failed" ) ;
563+ const stopFirst = vi . fn ( ( ) => {
564+ throw firstError ;
565+ } ) ;
566+ const stopSecond = vi . fn ( ( ) => {
567+ throw secondError ;
481568 } ) ;
482569
483570 const handle = await startTrackingServices ( {
@@ -486,12 +573,13 @@ describe("startPluginServices", () => {
486573 failOnStart : true ,
487574 stopSpy : vi . fn ( ) ,
488575 } ) ,
576+ createTrackingService ( "service-stop-first" , { stopSpy : stopFirst } ) ,
489577 createTrackingService ( "service-ok" , { stopSpy : stopOk } ) ,
490- createTrackingService ( "service-stop-fail " , { stopSpy : stopThrows } ) ,
578+ createTrackingService ( "service-stop-second " , { stopSpy : stopSecond } ) ,
491579 ] ,
492580 } ) ;
493581
494- await handle . stop ( ) ;
582+ await expect ( handle . stop ( ) ) . resolves . toBeUndefined ( ) ;
495583
496584 expect ( mockedLogger . error . mock . calls ) . toEqual ( [
497585 [
@@ -500,10 +588,12 @@ describe("startPluginServices", () => {
500588 ] ) ;
501589 expect ( requireLoggerErrorMessage ( ) ) . not . toContain ( "\n" ) ;
502590 expect ( mockedLogger . warn . mock . calls ) . toEqual ( [
503- [ "plugin service stop failed (service-stop-fail): Error: stop failed" ] ,
591+ [ "plugin service stop failed (service-stop-second): Error: second stop failed" ] ,
592+ [ "plugin service stop failed (service-stop-first): Error: first stop failed" ] ,
504593 ] ) ;
505594 expect ( stopOk ) . toHaveBeenCalledOnce ( ) ;
506- expect ( stopThrows ) . toHaveBeenCalledOnce ( ) ;
595+ expect ( stopFirst ) . toHaveBeenCalledOnce ( ) ;
596+ expect ( stopSecond ) . toHaveBeenCalledOnce ( ) ;
507597 } ) ;
508598
509599 it ( "continues starting siblings when rollback also fails" , async ( ) => {
0 commit comments