Skip to content
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

Fix mmap.resize errors #1872

Merged
merged 3 commits into from
Jan 15, 2025
Merged

Conversation

BCSharp
Copy link
Member

@BCSharp BCSharp commented Jan 14, 2025

No description provided.

@BCSharp BCSharp marked this pull request as draft January 14, 2025 05:57
@BCSharp BCSharp marked this pull request as ready for review January 15, 2025 03:45
Copy link
Contributor

@slozier slozier left a comment

Choose a reason for hiding this comment

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

Thanks!

@slozier slozier merged commit 1a90cb4 into IronLanguages:main Jan 15, 2025
8 checks passed
@BCSharp BCSharp deleted the mmap_resize_errors branch January 15, 2025 19:21
@BCSharp
Copy link
Member Author

BCSharp commented Jan 15, 2025

I wanted to do more but I realized that I do not understand what the goal is here. Normally, when there is an problem with the code it is kind of evident what the original intention of it was. But here, is it a bug or a feature? Is the interface of mmap supposed to be thread-safe or not?

If yes, then how come all the effort is in making close thread-safe with concurrent execution of other regular methods (it wasn't, and it still is not), but not the regular methods between themselves? (MmapLocker is not locking anything.) Especially resize vs. other methods? I've never seen a pattern like that, when closing is (supposed to be) safe concurrently with the regular usage, but the regular usage is not thread-safe. It is normally the opposite: all methods are thread-safe, except closing/disposing, which has to happen after all usage ceases.

If not, why all this heavy lifting by locks, interlocked inc/dec, reference counting etc. If the interface is need not be thread-safe, all this overhead can go and simplify things considerably.

@slozier, do you know or suspect what the requirements are for the mmap interface? And is the .NET MemoryMappedFile/MemoryMappedViewAccessor thread-safe?

Right now what we have is a not-thread-safe interface with a lot of extra complexity that does not provide anything useful.

@slozier
Copy link
Contributor

slozier commented Jan 16, 2025

do you know or suspect what the requirements are for the mmap interface? And is the .NET MemoryMappedFile/MemoryMappedViewAccessor thread-safe?

I have to admit I haven't a clue why the MmapLocker exists. Other than doing the EnsureOpen check (which I guess we'd have to do everywhere anyway), the _refCount increment/decrement does not seem to serve a purpose. I guess it ensures some sort of consistent closed state? As for the mmap module itself I could imagine wanting to use multiple threads with it. However I would argue that if a program is doing stuff to alter the internal state (resize and close) while doing multi-threaded use of the same mmap then I don't think that's our problem? I don't know the thread-safety of the .NET classes, but at a glance it seems like having multiple threads using the same accessor would be fine.

@BCSharp
Copy link
Member Author

BCSharp commented Jan 17, 2025

Thanks for your insights. So my best guess now is that perhaps mmap was work in progress towards thread-safety that was abandoned half-way. Given this assumption and that having thread-safety is desirable, I think the best way moving forward is to embrace MmapLocker and at least make close thread-safe and indeed guaranteeing a consistent closed state. It should be simple (well, relatively speaking) esp. in comparison to resize, for two main reasons:

  1. close changes the state of mmap irreversibly, so incoming calls on other threads do not need to wait. They will be either let in or thrown out with an exception (ODE). OTOH resize will require some sort of locking, because after resize ends, mmap becomes usable again.
  2. close has no parameters. The only information it conveys is that it happened, which means literally just one bit of information. It will be easy to combine it with other data like reference counting in an atomic operation, without resorting to expensive locking.

Also, the difference between close and resize is that resize is discretionary — so we can expect the user code to take some precautions, or at least take it into consideration. OTOH close is, if not mandatory, then highly expected to be performed for each mmap created, so anything that guarantees a consistent closed state is a win.

I will come up with a solution for close in one of the next PRs. I do appreciate the efforts in MmapLocker to avoid locking as much as possible, especially in regular use cases. This is possible for close but will not be possible for resize.

@BCSharp BCSharp mentioned this pull request Jan 19, 2025
@BCSharp BCSharp mentioned this pull request Jan 29, 2025
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.

2 participants