Conversation
Fixes strange fclose() return values
The semaphore might be immediately reused by a file newly opened in another thread, thus creating a race condition.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes D issue 13727
https://issues.dlang.org/show_bug.cgi?id=13727
The root of the problem is that
UnlockSemaphoreNestedis messing with shared resources after releasing the lock to them.lock dec _iSemLockCtrs[edx * 2]is the point at which the lock is effectively released, however the macro proceeds to call_DestroySemaphoreafter releasing said lock. This ends up badly when another thread simultaneously acquires this semaphore (for another file) and attempts to use it.The solution I went for is to simply disable deleting semaphores on
fclose(or, well, when__fpunlockdetects anfclose). The side effect, in its worst case (opening and then closing_NFILEfiles), is a temporary leak of up to_NFILEsemaphores until the program exits.I'd like to add that the synchronization code generally raises red flags from a number of other points:
GetTickCountdoesn't actually even have millisecond precision)_EnsureSemaphore, called every time from_WaitSemaphoreand_ReleaseSemaphore, performs an unconditionalitoaand a questionable unsynchronized loopI think ideally it should be replaced by plain
CRITICAL_SECTIONobjects (which, AFAIU, function much like the good parts of the current implementation - spin locks + notification-based synchronization), but I guess overly invasive changes are best avoided here.