Skip to content

[Messenger] BatchHandlerTrait::flush() ignores $force parameter #61292

@wazum

Description

@wazum

Symfony version(s) affected

6.4, 7.4

Description

The BatchHandlerTrait::flush(bool $force) method completely ignores the $force parameter and always flushes when jobs exist, violating the documented interface contract.

Current Behavior

// Both calls behave identically - always flush if jobs exist
handler->flush(false); // Should only flush if shouldFlush() returns true
handler->flush(true);  // Should always flush (correct)

Expected Behavior

  • flush(false): Should only flush when shouldFlush() returns true (batch size reached)
  • flush(true): Should always flush (current behavior is correct)

Impact

  • Breaks efficient batching when messages arrive with gaps
  • Violates BatchHandlerInterface documentation: "it can be skipped if not"
  • Worker's flush(false) calls during idle periods flush prematurely

How to reproduce

Here's a test project: https://github.com/wazum/symfony-messenger-batch

Start the consumer first (with a limit of 20) and then call the command (with a batch size of 12) and you will see a random output like:

 // The worker will automatically exit once it has processed 20 messages or received a stop signal via the
 // messenger:stop-workers command.

 // Quit the worker with CONTROL-C.

 // Re-run the command with a -vv option to see logs about consumed messages.

Processed new batch of cat facts
------------------------------------------------------------
1. The female cat reaches sexual maturity within 6 to 10 months; most veterinarians suggest spaying the female at 5 months, before her first heat period. The male cat usually reaches sexual maturity between 9 and 12 months.
2. While it is commonly thought that the ancient Egyptians were the first to domesticate cats, the oldest known pet cat was recently found in a 9,500-year-old grave on the Mediterranean island of Cyprus. This grave predates early Egyptian art depicting cats by 4,000 years or more.
3. The first cat show was organized in 1871 in London. Cat shows later became a worldwide craze.


Processed new batch of cat facts
------------------------------------------------------------
1. Blue-eyed, white cats are often prone to deafness.
2. Cat's urine glows under a black light.
3. A queen (female cat) can begin mating when she is between 5 and 9 months old.
4. It has been scientifically proven that stroking a cat can lower one's blood pressure.
5. The claws on the cat’s back paws aren’t as sharp as the claws on the front paws because the claws in the back don’t retract and, consequently, become worn.
6. Cats must have fat in their diet because they can't produce it on their own.
7. A kitten will typically weigh about 3 ounces at birth.  The typical male housecat will weigh between  7 and 9 pounds, slightly less for female housecats.


Processed new batch of cat facts
------------------------------------------------------------
1. In contrast to dogs, cats have not undergone major changes during their domestication process.
2. Cats prefer to remain non-confrontational. They will not fight to show dominance, but rather to stake their territory. Cats will actually go to extremes to avoid one another in order to prevent a possible confrontation.
3. There are up to 60 million feral cats in the United States alone.
4. Cats lose almost as much fluid in the saliva while grooming themselves as they do through urination.

instead of the expected

 // The worker will automatically exit once it has processed 20 messages or received a stop signal via the
 // messenger:stop-workers command.

 // Quit the worker with CONTROL-C.

 // Re-run the command with a -vv option to see logs about consumed messages.

Processed new batch of cat facts
------------------------------------------------------------
1. Blue-eyed, white cats are often prone to deafness.
2. Cat's urine glows under a black light.
3. A queen (female cat) can begin mating when she is between 5 and 9 months old.
4. It has been scientifically proven that stroking a cat can lower one's blood pressure.
5. The claws on the cat’s back paws aren’t as sharp as the claws on the front paws because the claws in the back don’t retract and, consequently, become worn.
6. Cats must have fat in their diet because they can't produce it on their own.
7. A kitten will typically weigh about 3 ounces at birth.  The typical male housecat will weigh between  7 and 9 pounds, slightly less for female housecats.
8. In contrast to dogs, cats have not undergone major changes during their domestication process.
9. Cats prefer to remain non-confrontational. They will not fight to show dominance, but rather to stake their territory. Cats will actually go to extremes to avoid one another in order to prevent a possible confrontation.
…

Possible Solution

None yet

Additional Context

Test project (with ddev): https://github.com/wazum/symfony-messenger-batch

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions