A access violation caused by race condition

Posted by Kahenraz on Wed 21 Sep 2016 03:01 AM — 10 posts, 39,143 views.

#0
I have an external script file which I have set to automatically recompile changes "always" so that it's faster to test and debug with an external editor.

The problem I'm encountering is that sometimes MUSH will report a sharing violation:

---------------------------
MUSHclient
---------------------------
A sharing violation occurred while accessing C:\data\games\Mush\worlds\scripts\functions.lua.
---------------------------
OK
---------------------------

This is most likely due MUSH trying to read from the file while my editor is still writing to it.

A workaround for this would be to retry on a delay (sleep every 10ms or so) and if there is still an access violation after say, 3-5 seconds then report the access violation.
USA Global Moderator #1
Huh. This is the sort of thing I expect my OS to abstract away.
#2
You can't abstract away an IO error because it's blocking.
USA Global Moderator #3
Quote:
You can't abstract away an IO error because it's blocking.

Eh. None of that needs to be true with only one writer.
#4
You can't always read from a file while it's being written to. That's the race condition.

MUSH tries to detect changes in the file but doesn't handle instances where the write isn't atomic and needs to wait until the read is finished.

Just keep trying to read the file on a delay and with a reasonable timeout.

A simpler alternative is to copy the file to a unique temporary file and read it from there. I can't confirm but I'm pretty sure that the copy will block until it has a valid file handle. Just remember to delete the copy afterwards.
Amended on Wed 21 Sep 2016 06:59 PM by Kahenraz
Australia Forum Administrator #5
Quote:

A access violation caused by race condition


Do you mean a sharing violation? They aren't the same thing.

Some editors always write to a temporary file and rename at the last moment.
#6
I'm not certain where the error is coming from exactly but it happens between the time I save the file and MUSH tries to load it.

The error is annoying because I then have to switch to MUSH and click the little box to close before I can continue.
Australia Forum Administrator #7
You can turn off auto-reloading and just hit Shift+Ctrl+R when you are ready.
#8
I like the auto-reload functionality because it's easier to just reload the script every time I make a change.

All I want is for the client to make an effort to recover from this error rather than throwing up the message box outright.
Australia Forum Administrator #9
Well, this is the first time this was reported in over 10 years. I don't really want to build in a delay, or have the added complexity of noticing a file changes, but keeping on trying to read it.

I've changed it from a dialog box to a message in the output window. If you see the error message, then you can force a reload.


https://github.com/nickgammon/mushclient/commit/aa0cd16