[GH-ISSUE #353] [BUG] Dangling program #2180

Closed
opened 2026-03-15 19:34:42 +03:00 by kerem · 1 comment
Owner

Originally created by @1337-sys on GitHub (Nov 20, 2021).
Original GitHub issue: https://github.com/hibiken/asynq/issues/353

Originally assigned to: @hibiken on GitHub.

Program could not be closed. Previously I was calling the Stop() and Shutdown() funcs. Same result whether I call it or not. Asynq is already handling the shutdown job. Even though I set ShutdownTimeout, nothing changed.

^Casynq: pid=14201 2021/11/20 00:09:15.321915 INFO: Starting graceful shutdown
asynq: pid=14201 2021/11/20 00:09:15.322501 INFO: Stopping processor
asynq: pid=14201 2021/11/20 00:09:15.862248 INFO: Waiting for all workers to finish...
asynq: pid=14201 2021/11/20 00:09:15.862310 INFO: All workers have finished
asynq: pid=14201 2021/11/20 00:09:15.862259 INFO: Processor stopped
asynq: pid=14201 2021/11/20 00:09:15.862582 INFO: Starting graceful shutdown
asynq: pid=14201 2021/11/20 00:09:15.868641 INFO: Exiting

^C^C^C^C^C^C

^C^C^C^C^C^C^C^C^C^C^C^C^C

Initialize:

srv := asynq.NewServer(
		AsyncQueueCenter.redisConnOpt,
		asynq.Config{Concurrency: 50},//, ShutdownTimeout: time.Second * 10},
	)

	mux := asynq.NewServeMux()
	mux.Handle(q_name, NewOldMessageConsumer())

	w.mux = mux
	w.consumer = srv

	go func() {
		if err := srv.Run(mux); err != nil {
			err = errors.Wrapf(err, "Error when srv.Run(mux) %s", q_name)
			flog.GetLogger().Error(err)
			notification.SendNotifySlack(err.Error(), "ERROR")
			return
		}
	}()

Destroy:

// Trigger when CTRL+C 
func CloseQueueCenter() {
	flog.GetLogger().Infof("Stopping producer..\n")
	if err := AsyncQueueCenter.producer.Close(); err != nil { // One producer
		flog.GetLogger().Errorf("Stopping producer err: %v", err)
	}

	for instance_id, queues := range AsyncQueueCenter.Queues {
		for queueType, queue := range queues {
			queue.consumer.Stop()
			queue.consumer.Shutdown()
			flog.GetLogger().Infof("Stopped consumer %s -> %s..\n", instance_id, queueType)
		}
	}
	flog.GetLogger().Infof("Stopped queue center..\n")
}

Machine: Darwin Kernel Version 20.6.0 - MacBook Pro (Retina, 15-inch, Mid 2014) - MacOS Big Sur

Originally created by @1337-sys on GitHub (Nov 20, 2021). Original GitHub issue: https://github.com/hibiken/asynq/issues/353 Originally assigned to: @hibiken on GitHub. Program could not be closed. Previously I was calling the Stop() and Shutdown() funcs. Same result whether I call it or not. Asynq is already handling the shutdown job. Even though I set ShutdownTimeout, nothing changed. ``` ^Casynq: pid=14201 2021/11/20 00:09:15.321915 INFO: Starting graceful shutdown asynq: pid=14201 2021/11/20 00:09:15.322501 INFO: Stopping processor asynq: pid=14201 2021/11/20 00:09:15.862248 INFO: Waiting for all workers to finish... asynq: pid=14201 2021/11/20 00:09:15.862310 INFO: All workers have finished asynq: pid=14201 2021/11/20 00:09:15.862259 INFO: Processor stopped asynq: pid=14201 2021/11/20 00:09:15.862582 INFO: Starting graceful shutdown asynq: pid=14201 2021/11/20 00:09:15.868641 INFO: Exiting ^C^C^C^C^C^C ^C^C^C^C^C^C^C^C^C^C^C^C^C ``` Initialize: ``` srv := asynq.NewServer( AsyncQueueCenter.redisConnOpt, asynq.Config{Concurrency: 50},//, ShutdownTimeout: time.Second * 10}, ) mux := asynq.NewServeMux() mux.Handle(q_name, NewOldMessageConsumer()) w.mux = mux w.consumer = srv go func() { if err := srv.Run(mux); err != nil { err = errors.Wrapf(err, "Error when srv.Run(mux) %s", q_name) flog.GetLogger().Error(err) notification.SendNotifySlack(err.Error(), "ERROR") return } }() ``` Destroy: ``` // Trigger when CTRL+C func CloseQueueCenter() { flog.GetLogger().Infof("Stopping producer..\n") if err := AsyncQueueCenter.producer.Close(); err != nil { // One producer flog.GetLogger().Errorf("Stopping producer err: %v", err) } for instance_id, queues := range AsyncQueueCenter.Queues { for queueType, queue := range queues { queue.consumer.Stop() queue.consumer.Shutdown() flog.GetLogger().Infof("Stopped consumer %s -> %s..\n", instance_id, queueType) } } flog.GetLogger().Infof("Stopped queue center..\n") } ``` Machine: Darwin Kernel Version 20.6.0 - MacBook Pro (Retina, 15-inch, Mid 2014) - MacOS Big Sur
kerem 2026-03-15 19:34:42 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@hibiken commented on GitHub (Nov 20, 2021):

@1337-sys Thank you for opening an issue.

You should be able to fix this by replacing the call to Server.Run with Server.Start. For more context, Server.Run takes care of calling Server.Stop and Sever.Shutdown. But in this case, since you are calling those methods yourself, you should use Server.Start to start the server. Also, you don't need to put that call in a separate goroutine since Server.Start won't block.

Note to myself:
The bug surfaces when you call Server.Shutdown concurrently while another goroutine is already shutting down the server. To fix, introduce another state ShutdownInProgress and skip the shutdown operation.

<!-- gh-comment-id:974657359 --> @hibiken commented on GitHub (Nov 20, 2021): @1337-sys Thank you for opening an issue. You should be able to fix this by replacing the call to `Server.Run` with `Server.Start`. For more context, `Server.Run` takes care of calling `Server.Stop` and `Sever.Shutdown`. But in this case, since you are calling those methods yourself, you should use `Server.Start` to start the server. Also, you don't need to put that call in a separate goroutine since `Server.Start` won't block. Note to myself: The bug surfaces when you call `Server.Shutdown` concurrently while another goroutine is already shutting down the server. To fix, introduce another state `ShutdownInProgress` and skip the shutdown operation.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/asynq#2180
No description provided.