Skip to content

[12.x] Fix closure in chain not dispatched after batch completes#59492

Closed
JoshSalway wants to merge 1 commit into
laravel:12.xfrom
JoshSalway:fix/chained-batch-closure-serialization-12x
Closed

[12.x] Fix closure in chain not dispatched after batch completes#59492
JoshSalway wants to merge 1 commit into
laravel:12.xfrom
JoshSalway:fix/chained-batch-closure-serialization-12x

Conversation

@JoshSalway
Copy link
Copy Markdown

Summary

Backport of #59491 to 12.x. Fixes a bug where a closure in Bus::chain is not dispatched when it follows a Bus::batch, throwing Call to undefined method Closure::getClosure().

Relates to laravel/serializable-closure#106

Problem

ChainedBatch::attachRemainderOfChainToEndOfBatch() captures the next chain item directly in the batch's finally callback. When the batch serializes this callback, the nested SerializableClosure inside $next->closure gets unwrapped to a raw Closure during the round-trip.

Solution

Re-serialize $next to a string before capturing it in the finally closure. The string is deserialized at dispatch time.

Test plan

  • Integration test: testClosureAfterBatchInChainIsDispatched in JobChainingTest.php

When Bus::chain contains a closure after a Bus::batch, the closure
fails with 'Call to undefined method Closure::getClosure()'.

This happens because attachRemainderOfChainToEndOfBatch() captures
the next chain item (a CallQueuedClosure containing a
SerializableClosure) directly in the batch's finally callback.
When the batch serializes this callback, the nested
SerializableClosure gets unwrapped to a raw Closure during the
round-trip.

Fix by re-serializing the next chain item to a string before
capturing it in the finally closure.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 1, 2026

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@JoshSalway

This comment was marked as spam.

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.

1 participant