Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upUse autoincrementing number for handler ids #105
Conversation
|
Instead of uuid, can we just have a i64 id that we just atomically increment when we need one? Would also mean no changes to anything but the platform backends. |
|
Valid point. We could do that, but imo if we did that we should definitely implement some sort of id reuse. The creation of the id reuse algorithm could take as much effort as switching Uuid, depending on the route taken. |
|
Why do we need id reuse? If a program allocated a new id every millisecond, it would take ~585 million years to exaust 2^64. I think that's a pretty good definition of "someone else's problem" ;) (1 per ns gives us 585 years, which is slightly more concerning, but only slightly.) |
|
Haha very vary true and a very good point... I though about that, and AFAIK you'd have to exhaust If the consensus is to use an auto incrementing |
|
It's (For production builds, it should produce the exact same machine code I think?) Either way, I think it will be more efficient and less invasive than using I'd say the actual Changing the handling of |
|
|
Use an Option<i64> for r_id instead of a plain i64, and use None instead of -1 to indicate the id does not exist in the handles.
eb72e53
to
b8fdf19
|
Updated to use an auto-incrementing |
|
The code looks good :-) (Aside from minor optimisations -- see below.) The PR title however doesn't fit any more; and perhaps more importantly, I'm a bit unhappy with the commit messages... The second commit in particular actually has three somewhat distinct changes: switching the type of the tokens returned by The third commit could mention that the The first patch ironically has the most explicit commit message, although it's the simplest change :-) Still, could add that this is in preparation for changing the type to Sorry for picking on formalities like this. I'm just pedantic in general; and I really like a readable history :-) As usual, feel free to ignore these remarks if you think them silly... On an unrelated note, it might be good to mention somewhere that the |
| for fd in hangups.iter() { | ||
| self.fdids.remove(fd); | ||
| unsafe { | ||
| libc::close(*fd); |
This comment has been minimized.
This comment has been minimized.
antrik
Nov 7, 2016
Contributor
While I do understand that we need to delay purging the entries from pollfds, I can't think of any reason for delaying the FD closing?...
Also, I just realised that fdids doesn't really need to be a HashMap: you could just put the IDs in a positional vector parallel to pollfds. Or if you keep it a HashMap, you can drop the fdids entries immediately; and then you won't need hangups any more, as you can then just use fdids to check which entries need to be retained in pollfds. Not sure which is more efficient -- and with the mio patch hopefully getting merged soon, it's probably not worth spending too much thought on it... Just pick whatever seems easier I'd say :-)
Use an auto incrementing u64 value as the OsIpcReceiver's id within the
OsIpcReceiverSet in the inprocess and unix modules.
- Change the type returned by OsIpcReceiverSet select and add to u64,
to delay overflow.
- Factor out the implementation of an auto-incrementing id
inprocess into os::Incrementor, to be used by other platforms.
- Use the Incrementor in the unix module to decouple the id of the
OsIpcReceiver from the file descriptor to avoid race conditions due
to file descriptor reuse.
|
Thanks again, great critiques! I clearly need to work on better commit messages |
| } | ||
| // Avoid the use of `self` in closue to avoid errors due to the | ||
| // mutable borrow of `self` at line 483 | ||
| let ids = &self.fdids; |
This comment has been minimized.
This comment has been minimized.
dlrobertson
Nov 8, 2016
Author
Collaborator
The borrow checker has beaten me... I don't really like this, but this was the best workaround I could think of. If anyone can think of a better workaround, please let me know!
Either way... It's only temporary until the mio PR lands.
This comment has been minimized.
This comment has been minimized.
KiChjang
Nov 8, 2016
Member
What happens if instead of doing an inline closure in line 483, you make a let-binding of a closure and pass it in as an argument to retain?
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
antrik
Nov 8, 2016
Contributor
I think I understand the situation; I don't understand though why you consider it a problem?...
It's not exactly uncommon to create temporary bindings to get around borrow conflicts. I'm not sure this even deserves a comment: it doesn't seem like an obscure situation that requires much of an explanation?...
(BTW, including line numbers in a comment is a bad idea: these are bound to change :-) )
On the other hand, I for my part would be tempted to add a comment explaining that the entries that got removed from fdids, because the channels got closed, also need to be purged from pollfds. (And perhaps even mention that we can only do that after we finished iterating pollfds?...) These are the kind of things that are probably not immediately obvious even to an experienced Rust programmer looking at this code for the first time.
(I am aware that the existing ipc-channel code doesn't have much in the manner of such explanations... Which I personally consider a pity.)
On a more nit-picky note, I'd drop the blank line after the temporary binding -- these two lines very much belong together. Also, I think it would actually be preferable to name the temporary as fdids as well...
This comment has been minimized.
This comment has been minimized.
dlrobertson
Nov 10, 2016
Author
Collaborator
(BTW, including line numbers in a comment is a bad idea: these are bound to change :-) )
Ha true! good call... Removed the line numbers
I also added a comment above the retain detailing the reason for the placement.
This comment has been minimized.
This comment has been minimized.
antrik
Nov 10, 2016
Contributor
BTW, just for explanation (in case it isn't clear): the issue here isn't the borrow checker being dumb, but rather it's about the way how closures are handled. They automatically close over any binding used within the closure's code -- in this case the binding is self. The compiler doesn't try to figure out that you are actually accessing only one sub-field. If you want only the sub-field to be closed over, you have to create an explicit binding for it... Which is precisely what you did :-)
|
Looks good to me :-) Now we just need someone to approve it... |
|
@bors-servo r=antrik |
|
|
Use autoincrementing number for handler ids Use `Uuid` instead of `i64` for the handler keys to ensure file descriptor number reuse doesn't cause errors, and make sure to close file descriptors. **NB:** The scope of the PR has changed to change the handler id's from a platform specific `i64` to an autoincrementing `u64`. `macos` has not yet been ported, and probably suffers from the same leak as the `unix` module did prior to this PR
|
|
|
Cool beans! I'll move back onto #94 |
|
Thanks for doing this work, it's awesome :) On Thu, Nov 10, 2016 at 05:22:13PM -0800, Dan Robertson wrote:
|
dlrobertson commentedOct 1, 2016
•
edited
Use
Uuidinstead ofi64for the handler keys to ensure file descriptor number reuse doesn't cause errors, and make sure to close file descriptors.NB: The scope of the PR has changed to change the handler id's from a platform specific
i64to an autoincrementingu64.macoshas not yet been ported, and probably suffers from the same leak as theunixmodule did prior to this PR