Skip to content

[9.x] Prevent serializing default values of queued jobs#41348

Merged
taylorotwell merged 1 commit intolaravel:9.xfrom
adriaanzon:job-serialization
Mar 7, 2022
Merged

[9.x] Prevent serializing default values of queued jobs#41348
taylorotwell merged 1 commit intolaravel:9.xfrom
adriaanzon:job-serialization

Conversation

@adriaanzon
Copy link
Contributor

When a job gets dispatched to the queue, right before deploying a new version of the code that contains an updated version of the job, it’s possible to run into deserialization errors when the queue starts again after deploying. For example, when you are upgrading your code base to use typed properties instead of doc blocks:

class SyncToExternalService implements ShouldQueue
{
    use SerializesModels;
    use Queueable;

    /** @var ExternalService */
    private $externalService;

    public function handle(ExternalService $externalService)
    {
        $this->externalService = $externalService;
    }
}

to

class SyncToExternalService implements ShouldQueue
{
    use SerializesModels;
    use Queueable;

    private ExternalService $externalService;

    public function handle(ExternalService $externalService)
    {
        $this->externalService = $externalService;
    }
}

Before deploying, the $externalService property will be included in the serialized object, with null as value. After deploying the change, serializing the object won’t include $externalService, because then it is an uninitialized typed property. When you now try unserializing an object that was created before deploying (which happens when you start the queue again and the queue still contains jobs from before the deploy), you will get the following error:

TypeError: Cannot assign null to property SyncToExternalService::$externalService of type ExternalService

This pull request circumvents this error by not including the property on the serialized object.

@taylorotwell taylorotwell merged commit 22c2cee into laravel:9.x Mar 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants