@@ -170,7 +170,8 @@ public synchronized Worker newWorker(String taskQueue, WorkerOptions options) {
170170 workflowClient .getOptions ().getContextPropagators ());
171171 workers .put (taskQueue , worker );
172172
173- // Go through the plugins to call plugin initializeWorker hooks (e.g. register workflows, activities, etc.)
173+ // Go through the plugins to call plugin initializeWorker hooks (e.g. register workflows,
174+ // activities, etc.)
174175 for (Object plugin : plugins ) {
175176 if (plugin instanceof WorkerPlugin ) {
176177 ((WorkerPlugin ) plugin ).initializeWorker (taskQueue , worker );
@@ -339,20 +340,33 @@ public synchronized void shutdownNow() {
339340 private void shutdownInternal (boolean interruptUserTasks ) {
340341 state = State .Shutdown ;
341342
342- // Notify plugins of shutdown (forward order)
343- for (Object plugin : plugins ) {
343+ // Build plugin shutdown chain (reverse order for proper nesting)
344+ Runnable shutdownChain = () -> doShutdown (interruptUserTasks );
345+ List <Object > reversed = new ArrayList <>(plugins );
346+ Collections .reverse (reversed );
347+ for (Object plugin : reversed ) {
344348 if (plugin instanceof WorkerPlugin ) {
345- try {
346- ((WorkerPlugin ) plugin ).onWorkerFactoryShutdown (this );
347- } catch (Exception e ) {
348- log .warn (
349- "Plugin {} failed during shutdown notification" ,
350- ((WorkerPlugin ) plugin ).getName (),
351- e );
352- }
349+ final Runnable next = shutdownChain ;
350+ final WorkerPlugin workerPlugin = (WorkerPlugin ) plugin ;
351+ shutdownChain =
352+ () -> {
353+ try {
354+ workerPlugin .shutdownWorkerFactory (this , next );
355+ } catch (Exception e ) {
356+ log .warn ("Plugin {} failed during shutdown" , workerPlugin .getName (), e );
357+ // Still try to continue shutdown
358+ next .run ();
359+ }
360+ };
353361 }
354362 }
355363
364+ // Execute the chain
365+ shutdownChain .run ();
366+ }
367+
368+ /** Internal method that actually shuts down workers. Called from the plugin chain. */
369+ private void doShutdown (boolean interruptUserTasks ) {
356370 ((WorkflowClientInternal ) workflowClient .getInternal ()).deregisterWorkerFactory (this );
357371 ShutdownManager shutdownManager = new ShutdownManager ();
358372 CompletableFuture .allOf (
0 commit comments