Skip to content

Fix - OutOfMemory error in StreamHandler #1578

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

Merged
merged 2 commits into from
Sep 14, 2021

Conversation

juan-morales
Copy link
Contributor

Fixes Issue #1577

This change is meant to prevent setting a stream chunk size bigger than the available amount of memory.

The default value is still set, but can get change after calculations based on the php settings and the memory currently used by the php process.

There is a hard limit for an stream size of 2G ... is does not matter if the PHP process gets 1000 G of memory, the stream size cannot be bigger than 2G! and I suspect is because of some C lib used behind the scenes to implement streams in PHP.

Also ... the official PHP documentation states that stream_set_chunk_size() can hold a value of PHP_INT_MAX , but this is not completely true, because no matter what, it cannot be bigger than 2G! (because of the reason I already mentioned.

I suspect that if maybe the libs used in PHP are compiled with a 64 bit version... maybe the size can get bigger than 2G ... but just to be safe ... I would prefer to keep it in 2G.

This topic is quite tricky, please, someone elses opinion on this? ... thanks in advance

Ref: PHP official documentation for stream_set_chunk_size()

@juan-morales
Copy link
Contributor Author

@Seldaek sorry to bother you. Can you help me with the phpstan errors I get in this PR?

@Seldaek
Copy link
Owner

Seldaek commented Aug 1, 2021

Feel free to ignore this for now, I need to find time to review the rest of the PR first, then will happily help with that.

@juan-morales
Copy link
Contributor Author

Feel free to ignore this for now, I need to find time to review the rest of the PR first, then will happily help with that.

Thanks for Your quick response.

@juan-morales
Copy link
Contributor Author

@Seldaek your last change fixed the problem I had on this pull request during the automatic checks. Thanks.

Comment on lines 70 to 76
try {
stream_set_chunk_size($this->stream, $this->streamChunkSize);
} catch (\Exception $exception) {
throw new \RuntimeException('Impossible to set the stream chunk size.'
.PHP_EOL.'Error: '.$exception->getMessage()
.PHP_EOL.'Trace: '.$exception->getTraceAsString());
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why rethrowing here and not simply letting the original exception do its thing?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this change for now as I don't understand it and I'd like to get the rest merged, if you think it's important please explain and we can do it in another PR.

/*
* At this point, stream_set_chunk_size() failed in the constructor.
* Probably because not enough memory.
* The rest of th test depends on the success pf stream_set_chunk_size(), that is why
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

several typos in that sentence

… and use lower amount of chunk size based on memory limit
@Seldaek Seldaek force-pushed the fix-oom-streamhandler branch from b5e0383 to b2d6af6 Compare September 14, 2021 12:57
@Seldaek Seldaek force-pushed the fix-oom-streamhandler branch from b2d6af6 to 70fe092 Compare September 14, 2021 13:01
@Seldaek
Copy link
Owner

Seldaek commented Sep 14, 2021

cc @dsch this reduces the chunk size again, but IMO it'll still be large enough in most cases to fit in enough data to make writes atomic

Thanks @juan-morales for the PR! I tweaked things a bit to make it all simpler and not dependent on the current memory usage, will let you take a look at my commit.

@Seldaek Seldaek merged commit 646f0c3 into Seldaek:main Sep 14, 2021
@Seldaek Seldaek added this to the 2.x milestone Sep 14, 2021
@Seldaek Seldaek added the Bug label Sep 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants