Skip to content

Conversation

@drealecs
Copy link

After the specific Flysystem Adapter wrote the stream, it might have closed it.
So we double-check before trying to close it again.

This happens for league/flysystem-aws-s3-v3 -> aws/aws-sdk-php -> guzzlehttp/psr7 and stream is wrapped in GuzzleHttp\Psr7\Stream that on destruct closes it.
Maybe they shouldn't close it, but we're not in control of it. So double-checking sounds reasonable.

@drealecs
Copy link
Author

drealecs commented Dec 17, 2025

Looking into it more, I found that this was also documented in https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html:

Additional info on behavior of the stream parameters: Psr7 takes ownership of streams and will automatically close streams when this method is called with a stream as the Body parameter. To prevent this, set the Body using GuzzleHttp\Psr7\stream_for method with a is an instance of Psr\Http\Message\StreamInterface, and it will be returned unmodified. This will allow you to keep the stream in scope.

The issue, for reference: aws/aws-sdk-php#929
Even if this looks like it could be fixed in other libraries, I think it's worth adding the simple check here.

@drealecs
Copy link
Author

We tested the patch in production and it fixed the issue with the fclose throwing an error because the stream was already closed.

Let me know if this PR needs anything more.

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