8000 [Messenger] memory-limit option inaccurate / memory_resolver not configurable · Issue #29957 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] memory-limit option inaccurate / memory_resolver not configurable #29957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
j-schumann opened this issue Jan 22, 2019 · 0 comments
Closed

Comments

@j-schumann
Copy link

Symfony version(s) affected: 4.2 with symfony/messenger v4.2.2

Description
Using the messenger:consume-messages command with -m 250M when memory_limit is set to 400M still crashes the worker.

How to reproduce
I have (obviously) some module (like PHPSpreadsheet) with memory leaks in use and each consumed message increases the memory usage by around 20-60MB. Even with a buffer of 150M the limit option does not prevent the memory exhaustion.

Possible Solution
I don't know if memory_get_usage(false) is accurate for other use cases than mine, else I would suggest to change the default memory_resolver to set the real_usage option to true.

If this is not possible because it would break other use cases it would be best to make the memory_resolver really configurable. He can be set in the StopWhenMemoryUsageIsExceededReceiver constructor but ConsumeMessagesCommand::execute uses a hardcoded constructor without the ability to inject a memory_resolver via a service or replacing the complete StopWhenTimeLimitIsReachedReceiver with a service.

Additional context
I think this is because the memory_resolver in StopWhenMemoryUsageIsExceededReceiver uses memory_get_usage() instead of memory_get_usage(true). My debug output is below, "before" is the value of memory_get_usage(true) when the MessageHandler starts executing, "X vs. Y" is my debug in StopWhenMemoryUsageIsExceededReceiver::receive, where X is the value of memory_get_usage() and Y is the value of the --memory-limit option used to call the script. As you can see memory_get_usage(true) is much closer to the real memory limit when the script crashes:

before: 65011712
76763096 vs. 262144000
before: 214433792
91614464 vs. 262144000
before: 225443840
120261384 vs. 262144000
before: 254279680
167287440 vs. 262144000
before: 305659904
204516872 vs. 262144000
before: 340262912
253016296 vs. 262144000
before: 402132992
Fatal error: Allowed memory size of 420478976 bytes exhausted ...

The unit tests dont use memory_get_usage at all so a real memory leak is not tested. I understand that it would be difficult to create a reproducible leak with predictable amout of memory usage where the discrepancy of memory_get_usage() and memory_get_usage(true) would show up.

@fabpot fabpot closed this as completed May 27, 2019
fabpot added a commit that referenced this issue May 27, 2019
…t (chalasr)

This PR was merged into the 4.2 branch.

Discussion
----------

[Messenger] Use "real" memory usage to honor --memory-limit

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29957
| License       | MIT
| Doc PR        | n/a

At least it's consistent with what other daemon-based libraries do:
https://github.com/php-enqueue/enqueue-dev/blob/master/pkg/enqueue/Consumption/Extension/LimitConsumerMemoryExtension.php#L58
https://github.com/M6Web/DaemonBundle/blob/master/src/M6Web/Bundle/DaemonBundle/Command/DaemonCommand.php#L493

Commits
-------

fbfe2df [Messenger] Use real memory usage for --memory-limit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
0