[GH-ISSUE #746] [BUG] Serializing/deserializing objects (eg: asynq.Client) into a task doesn't work as expected #2389

Open
opened 2026-03-15 20:19:52 +03:00 by kerem · 1 comment
Owner

Originally created by @dray92 on GitHub (Sep 22, 2023).
Original GitHub issue: https://github.com/hibiken/asynq/issues/746

Originally assigned to: @hibiken on GitHub.

Describe the bug
For testing purposes, I have a task obj that stores a reference to the asynq.Client object (I have tested w/ both pointer and non-pointer). When the task is deserialized and processed by the worker, the field for async.Client is not set.

To Reproduce
Steps to reproduce the behavior (Code snippets if applicable):

  1. Run redis
  2. Enqueue task that looks like this:
    type emailTaskPayload struct {
       // ID for the email recipient.
       UserID int
       Client asynq.Client // *asynq.Client
    }
    
  3. Handler looks like this (slightly modified from docs):
    func HandleWelcomeEmailTask(ctx context.Context, t *asynq.Task) error {
    var p emailTaskPayload
    if err := json.Unmarshal(t.Payload(), &p); err != nil {
    	return err
    }
    log.Printf(" [*] Send Welcome Email to User %d", p.UserID)
    
    log.Printf("actual deserialized client: %+v", p.Client)
    
    info, err := p.Client.Enqueue(asynq.NewTask(TypeWelcomeEmail, t.Payload()))
    if err != nil {
    	log.Fatal(err)
    }
    log.Printf(" [*] Successfully enqueued task: %+v", info)
    
    return nil
    } 
    
  4. See Error
    2023/09/22 01:17:25  [*] Send Welcome Email to User 42
    2023/09/22 01:17:25 actual deserialized client: {broker:<nil>}
    asynq: pid=4171748 2023/09/22 01:17:25.372155 ERROR: recovering from panic. See the stack trace below for details:
    goroutine 24 [running]:
    runtime/debug.Stack()
            /home/devzero/.goenv/versions/1.20.3/src/runtime/debug/stack.go:24 +0x65
            ....... more stuff
    

Expected behavior
Worker is able to deserialize the client object and use it to publish a new task.

Screenshots
If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

  • OS: Linux
  • Version of asynq package: v0.24.1

Additional context
Add any other context about the problem here.

Originally created by @dray92 on GitHub (Sep 22, 2023). Original GitHub issue: https://github.com/hibiken/asynq/issues/746 Originally assigned to: @hibiken on GitHub. **Describe the bug** For testing purposes, I have a task obj that stores a reference to the `asynq.Client` object (I have tested w/ both pointer and non-pointer). When the task is deserialized and processed by the worker, the field for `async.Client` is not set. **To Reproduce** Steps to reproduce the behavior (Code snippets if applicable): 1. Run redis 2. Enqueue task that looks like this: ``` type emailTaskPayload struct { // ID for the email recipient. UserID int Client asynq.Client // *asynq.Client } ``` 3. Handler looks like this (slightly modified from docs): ``` func HandleWelcomeEmailTask(ctx context.Context, t *asynq.Task) error { var p emailTaskPayload if err := json.Unmarshal(t.Payload(), &p); err != nil { return err } log.Printf(" [*] Send Welcome Email to User %d", p.UserID) log.Printf("actual deserialized client: %+v", p.Client) info, err := p.Client.Enqueue(asynq.NewTask(TypeWelcomeEmail, t.Payload())) if err != nil { log.Fatal(err) } log.Printf(" [*] Successfully enqueued task: %+v", info) return nil } ``` 4. See Error ``` 2023/09/22 01:17:25 [*] Send Welcome Email to User 42 2023/09/22 01:17:25 actual deserialized client: {broker:<nil>} asynq: pid=4171748 2023/09/22 01:17:25.372155 ERROR: recovering from panic. See the stack trace below for details: goroutine 24 [running]: runtime/debug.Stack() /home/devzero/.goenv/versions/1.20.3/src/runtime/debug/stack.go:24 +0x65 ....... more stuff ```` **Expected behavior** Worker is able to deserialize the client object and use it to publish a new task. **Screenshots** ~~If applicable, add screenshots to help explain your problem.~~ **Environment (please complete the following information):** - OS: Linux - Version of `asynq` package: v0.24.1 **Additional context** Add any other context about the problem here.
Author
Owner

@dray92 commented on GitHub (Sep 22, 2023):

The workaround when people face issues like these:

  • initializing the relevant clients as part of the worker process
  • running the worker process as part of the main process, maybe using goroutines etc
<!-- gh-comment-id:1730601393 --> @dray92 commented on GitHub (Sep 22, 2023): The workaround when people face issues like these: - initializing the relevant clients as part of the worker process - running the worker process as part of the main process, maybe using goroutines etc
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#2389
No description provided.