From 9b807477a124e9ddc2d984db72bcab24ff33f352 Mon Sep 17 00:00:00 2001 From: Marcin Maliszkiewicz Date: Wed, 21 Jun 2023 20:54:25 +0200 Subject: [PATCH] fix(cmd): stop generators as last to avoid deadlock on exit As jobs unconditionally read from generator's channel there can be a situation when generator is closed and will not produce any new values while some jobs still wait on empty channel deadlocking and preventing process exit. --- cmd/gemini/root.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/gemini/root.go b/cmd/gemini/root.go index 768b2f78..8f70e979 100644 --- a/cmd/gemini/root.go +++ b/cmd/gemini/root.go @@ -259,7 +259,8 @@ func run(_ *cobra.Command, _ []string) error { stop.StartOsSignalsTransmitter(logger, &warmupStopFlag, &workStopFlag) pump := jobs.NewPump(ctx, logger) - generators := createGenerators(ctx, schema, schemaConfig, distFunc, concurrency, partitionCount, logger) + genCtx, genCancel := context.WithCancel(context.Background()) + generators := createGenerators(genCtx, schema, schemaConfig, distFunc, concurrency, partitionCount, logger) if !nonInteractive { sp := createSpinner(interactive()) @@ -294,8 +295,13 @@ func run(_ *cobra.Command, _ []string) error { if err = jobsList.Run(ctx, schema, schemaConfig, st, pump, generators, globalStatus, logger, seed, &workStopFlag, failFast, verbose); err != nil { logger.Debug("error detected", zap.Error(err)) } - } + + // stop generators, this needs to be done after jobs finish + // because otherwise they can deadlock in pick() func waiting + // for more values to be generated + genCancel() + logger.Info("test finished") globalStatus.PrintResult(outFile, schema, version) if globalStatus.HasErrors() {