-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Throw a StateError if ResourceHandle.to* is called more than once. #49878
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
Comments
How would people know if |
This should not impact ACX. To @devoncarew's point, can we make this a compilation error or a lint? |
Overall LGTM. Is it also worth considering caching the object via weak ref? |
I know nothing about |
@devoncarew We have scaring language in the
Other than that I'm not sure how safe we can make this - the use case is receiving a file descriptor from another process and then converting it in a socket/file/pipe/whatever. |
@vsmenon Do you think that people will be surprised if we cache the file? The file is mutable so calling it twice will result in sharing state. We'd also still have the issue that issues cannot do: rh.toFile();
rh.toRawSocket(); // File descriptor already in use by the file! |
It sounds like it's not a high volume use case, and that the expectation is that people will create the object, and convert it to another type, likely within one method? Otherwise it seems like it would be tough - if you were given a Perhaps that issue could could be mitigated by API dartdocs? If we doc'd this behavior, then people using the API would be aware that those methods aren't idempotent and could plan for it. |
The actual PR has includes documentation changes:
I should probably change s/must/can/ . PR is here: |
Fixed in fa53fb6 |
Change
Throw a StateError if
ResourceHandle.toFile()
,ResourceHandle.toSocket()
,ResourceHandle.toRawSocket()
orResourceHandle.toRawDatagramSocket()
is called more than once.Rationale
Calling
ResourceHandle.toFile()
,ResourceHandle.toSocket()
,ResourceHandle.toRawSocket()
orResourceHandle.toRawDatagramSocket()
more than once produces two distinct Dart objects that are backed by the same file descriptor, which will become invalid when one object is closed or GCed.For example:
Impact
It is unlikely that this will break any existing correct code.
Mitigation
Users can save the result of the
ResourceHandle.toFile()
, etc. and use that instead of callingResourceHandle.toFile()
again.The text was updated successfully, but these errors were encountered: