framework: Job not logged in failed_jobs if timeout occurs within database transaction

Laravel Version

10.37.3

PHP Version

8.2.7

Database Driver & Version

MariaDB 10.3.27

Description

We had a ‘phantom’ problem that jobs that failed, were not logged to the failed_jobs table. After some investigation we found out that by mistake we were doing a heavy process within a db transaction which lead the specific job to timeout.

After testing we found out that the issue only happens if the jobs timeout during an open db transaction

I reported this in laravel/horizon but it turns out it has nothing to do with horizon but seems to be a framework issue.

Steps To Reproduce

  • Create a job with a timeout of 5 seconds
  • In the job handle method sleep within a db transaction
    public function handle(): void
    {
        DB::transaction(function (): void {
            sleep(10);
        });
    }

We would expect a job in the failed_jobs table, however this is not the case, and the JobFailed event is also not dispatched

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 26 (12 by maintainers)

Commits related to this issue

Most upvoted comments

I can confirm that the mentioned PR, which by now is released, fixes this bug.

@decaylala we are very grateful for your effort in proposing the solution which was eventually accepted!

@driesvints thx for initially labelling as bug even if later on the issue was closed without confirmation of reproduction. It certainly helped to finally attract somebody with the proper skills to fix this once and for all.

We will be testing this in the coming weeks when its released.

@crynobone Hi, we are experiencing the same issue. It seems that the QueueTransactionTest does not fully cover this scenario.

First, if the Batchable trait is removed from Fixtures\TimeOutJobWithTransaction, the test fails. Specifically, when using the SQS queue driver, the timedout job is deleted and not logged, which results in its loss.

<?php

namespace Illuminate\Tests\Integration\Database\Queue\Fixtures;

use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\DB;

class TimeOutJobWithTransaction implements ShouldQueue
{
    // without Batchable trait
    use InteractsWithQueue, Queueable;

    public int $tries = 1;
    public int $timeout = 2;

    public function handle(): void
    {
        DB::transaction(fn () => sleep(20));
    }
}

In addition, when nested transactions are involved, even with the Batchable trait, not all transactions are rolled back completely. This leads to the same issue.

<?php

namespace Illuminate\Tests\Integration\Database\Queue\Fixtures;

use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\DB;

class TimeOutJobWithTransaction implements ShouldQueue
{
    use InteractsWithQueue, Queueable, Batchable;

    public int $tries = 1;
    public int $timeout = 2;

    public function handle(): void
    {
        // nested transactions
        DB::transaction(function () {
            DB::transaction(fn () => sleep(20));
            sleep(20);
        });
    }
}

@crynobone we use laravel horizon which automatically handles that. But please be aware: If the job had $tries=1 (which most of our jobs do) the job is NOT retried, and NOT logged, and therefore lost.

Please correct my if i’m wrong. This is the whole point of this ticket - loosing jobs

@gkmk Thank you for your insight and suggestions.

The reason i would tend to shy away from option 1) is because then behaviour would be different between jobs that are batchable, and ones that are not. This would be an unnecessary inconsistency in my opinion. (See my previous comment)

On a more subjective note, we have > 100 jobs, most of them have transactions and do not require failed methods. Option 2) would add failed methods just to handle this specific case to not loose jobs, which seems to be quite an overhead.

Will be interesting to see what the core of laravel developers have to say.