neoq: Panic when moving to neoq_dead_jobs
Thank you for the patch for https://github.com/acaloiaro/neoq/issues/85, however this gets me to the next problems.
First, when a failed job exceeds the max retries, it is to be moved to the neoq_dead_jobs table, however a panic is being generated there:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x1a53181]
goroutine 118 [running]:
github.com/acaloiaro/neoq/backends/postgres.(*PgBackend).moveToDeadQueue(0xc000774150?, {0x20bd150, 0xc000774150}, {0x20c4300, 0xc0000126a8}, 0xc0001f5680, {0x0, 0x0})
/Users/billmatlock/go/pkg/mod/github.com/acaloiaro/neoq@v0.36.0/backends/postgres/postgres_backend.go:442 +0xe1
github.com/acaloiaro/neoq/backends/postgres.(*PgBackend).updateJob(0xc0003f4000, {0x20bd150, 0xc000774150}, {0x0, 0x0})
/Users/billmatlock/go/pkg/mod/github.com/acaloiaro/neoq@v0.36.0/backends/postgres/postgres_backend.go:484 +0x2c5
github.com/acaloiaro/neoq/backends/postgres.(*PgBackend).handleJob(0xc0003f4000, {0x20bd188, 0xc00039c370}, {0x28a0a38, 0x1}, {0xc000412150, 0x8, 0x6fc23ac00, 0x0, {0x1dc5b27, ...}})
/Users/billmatlock/go/pkg/mod/github.com/acaloiaro/neoq@v0.36.0/backends/postgres/postgres_backend.go:723 +0x4ce
github.com/acaloiaro/neoq/backends/postgres.(*PgBackend).start.func2()
/Users/billmatlock/go/pkg/mod/github.com/acaloiaro/neoq@v0.36.0/backends/postgres/postgres_backend.go:539 +0x1db
created by github.com/acaloiaro/neoq/backends/postgres.(*PgBackend).start in goroutine 5
/Users/billmatlock/go/pkg/mod/github.com/acaloiaro/neoq@v0.36.0/backends/postgres/postgres_backend.go:532 +0x353
I believe this is due to the fact that jobErr is not being populated, causing the nil pointer dereference. However the error message is also in the job struct so maybe the fix would be to drop the jobErr param and set the error field to the value of j.Error ?
In /backends/postgres/postgres_backend.go moveToDeadQueue()
Go from:
_, err = tx.Exec(ctx, `INSERT INTO neoq_dead_jobs(id, queue, fingerprint, payload, retries, max_retries, error, deadline)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
j.ID, j.Queue, j.Fingerprint, j.Payload, j.Retries, j.MaxRetries, jobErr.Error(), j.Deadline)
To:
_, err = tx.Exec(ctx, `INSERT INTO neoq_dead_jobs(id, queue, fingerprint, payload, retries, max_retries, error, deadline)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
j.ID, j.Queue, j.Fingerprint, j.Payload, j.Retries, j.MaxRetries, j.Error, j.Deadline)
Secondly, if I patch the above error as described to get through the panic, the next time the failed job is ran, it is being ran on a different queue (handler) than what is assigned to it. Here is a snippet of my testing log showing that the first time it is getting handled by the correct handler (nqTestHandlerWithErr()) but the second time it is not (nqTestHandler()), but I do not know if my changes above are causing this or not, so I did not open a separate issue:
=== RUN TestNeoq/TestAddJobs
2023/10/13 11:10:22 nqTestHandler()
2023/10/13 11:10:22 got job id: 1 queue: test_now messsage: [test_now] this is an instant job
2023/10/13 11:10:22 waiting 10 seconds for delayed job to run...
2023/10/13 11:10:27 nqTestHandler()
2023/10/13 11:10:27 got job id: 2 queue: test_delayed messsage: [test_delayed] this is a delayed job
2023/10/13 11:10:32 waiting 3 seconds for failed job to run...
2023/10/13 11:10:32 nqTestHandlerWithErr()
2023/10/13 11:10:32 got job id: 3 queue: test_error messsage: [test_error] this is a job with errors
time=2023-10-13T11:10:32.495-04:00 level=ERROR msg="job failed" job_error="job failed to process: this is a test error"
2023/10/13 11:10:35 waiting 30 seconds for failed job to get retried...
2023/10/13 11:10:48 nqTestHandler() <<<<< should be `nqTestHandlerWithErr()`
2023/10/13 11:10:48 got job id: 3 queue: test_error messsage: [test_error] this is a job with errors
And here are the test handlers:
var (
ErrTest = errors.New("this is a test error")
)
func nqTestHandler(db *database.DB) handler.Func {
return func(ctx context.Context) (err error) {
var j *jobs.Job
j, err = jobs.FromContext(ctx)
if err != nil {
return
}
log.Println("nqTestHandler()")
log.Println("got job id:", j.ID, "queue:", j.Queue, "messsage:", j.Payload["message"])
return
}
}
func nqTestHandlerWithErr() handler.Func {
return func(ctx context.Context) (err error) {
j, err := jobs.FromContext(ctx)
if err != nil {
log.Printf("ERROR in nqTestHandlerWithErr: %v", err)
}
log.Println("nqTestHandlerWithErr()")
log.Println("got job id:", j.ID, "queue:", j.Queue, "messsage:", j.Payload["message"])
return ErrTest
}
}
Thank you!
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (11 by maintainers)
Commits related to this issue
- fix: #98 segvault when moving jobs to dead queue — committed to acaloiaro/neoq by acaloiaro 3 years ago
- fix: #98 retries can be picked up by wrong handler — committed to acaloiaro/neoq by acaloiaro 3 years ago
- fix: #98 retries can be picked up by wrong handler — committed to acaloiaro/neoq by acaloiaro 3 years ago
- fix: #98 retries can be picked up by wrong handler — committed to acaloiaro/neoq by acaloiaro 3 years ago
- fix: #98 retries can be picked up by wrong handler — committed to acaloiaro/neoq by acaloiaro 3 years ago
- fix: #98 retries can be picked up by wrong handler Fixes a bug that can allow retries to end up on the wrong queue in settings where there are multiple handlers. — committed to acaloiaro/neoq by acaloiaro 3 years ago
- fix: #98 retries can be picked up by wrong handler Fixes a bug that can allow retries to end up on the wrong queue in settings where there are multiple handlers. — committed to acaloiaro/neoq by acaloiaro 3 years ago
Hi Bill, I’m traveling but will take a look at your data when I can.
On Mon, Oct 16, 2023, at 11:09, Bill Matlock wrote:
Thanks for the report @BillBuilt. I’ll look into this soon.