This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Draradech's profile picture

[Question] program interruption on server stop

Started by Draradech, 15 January 2013 - 10:33 PM
Draradech #1
Posted 15 January 2013 - 11:33 PM
Where in my code can the program be interrupted (for a] clean server shutdown, b] server crash/kill)?

For b] the answer is probably "anywhere"?

Now for a]… I would guess only in places where it yields? If so, is there a comprehensive list as to which functions yield and which ones don't? Especially the file io ones?
If the answer is "anywhere" as well, is it possible at all to build a restart-proof persistence system?
Doyle3694 #2
Posted 16 January 2013 - 01:08 AM
I would guess it's anywhere for a as well. Since , otherwise, a server shutdown prevention machine wouldn't be hard to make
Draradech #3
Posted 16 January 2013 - 02:05 AM
Programs which don't yield are terminated with a "too long without yielding" message. That would make it possible to shut down a server, even if programs will only be terminated when they yield (or after too long without yielding).

Does anybody know for sure what the answer is?
remiX #4
Posted 16 January 2013 - 02:40 AM
Well what's in the code…

If it's variables and such, save to a file and then reload them on startup.
Draradech #5
Posted 16 January 2013 - 02:44 AM
Of course, but thats not the question. Assume a turtle is moving forward like this (with save and get some kind of persistence system):

turtle.forward()
save("posX", get("posX") + 1)

The problem is the following:

turtle.forward()
-- server gets shut down here --
save("posX", get("posX") + 1)

The turtle now doesn't know that it moved. Is there any way around that?
theoriginalbit #6
Posted 16 January 2013 - 02:51 AM
The turtle now doesn't know that it moved. Is there any way around that?
GPS… :P/>

Also i think you would want to use the file mode "w+" just incase its shutdown after the file handler is open, but before its flushed. "w+" is meant to only update the data on close from memory. where as "w" clears the file on open… or something like that…
Zoinky #7
Posted 16 January 2013 - 02:59 AM
Save the coords that it is moving to and then move the turtle. If a server shuts down in the middle of a turtle movement the turtle will move/turn once the server gets loaded again. Saving is basically instant, unlike moving. I would do more explaining, but I think you get the point.
Draradech #8
Posted 16 January 2013 - 03:00 AM
GPS… :P/>
Okay. Now lets say said turtle needs to run unattended far into the distance and has a means to keep chunks loaded (either via a MiscPeripherals chunkloader turtle, or by carrying world anchors with it and placing them apropriately). GPS is not really an option then.

Save the coords that it is moving to and then move the turtle. If a server shuts down in the middle of a turtle movement the turtle will move/turn once the server gets loaded again. Saving is basically instant, unlike moving. I would do more explaining, but I think you get the point.
Yes, but then it would have moved twice and counted only one, which is just as bad. Edit2: Wait, what? Its the other way round, but my point still stands.
Edit: also, I can't know in advance if moving is even possible, thats what the return code of turtle.forward() is for…

Either way, that is an interesting discussion, but it ultimately does not answer my question "where can programs be interrupted". If nobody knows I might need to get a test program running and just try it. I just thought somebody would know this, as there are supposedly restart-safe turtle programs available.
theoriginalbit #9
Posted 16 January 2013 - 03:11 AM
Okay. Now lets say said turtle needs to run unattended far into the distance and has a means to keep chunks loaded (either via a MiscPeripherals chunkloader turtle, or by carrying world anchors with it and placing them apropriately). GPS is not really an option then.
Wait for persistence to be implemented

As far as I'm aware a program on shutdown is not interrupted at any particular place, it is just terminated anywhere it can be… But I think one of the devs/mods may be better ppl to answer this question 100%
Lyqyd #10
Posted 16 January 2013 - 07:10 AM
It waits until the computer yields to wait for an event before it unloads the computer. All turtle movements (and most of the turtle actions, IIRC), sleep, read, os.pullEvent (and -Raw), rednet.receive, coroutine.yield (and more!) will all do this. Any coroutine running on any given computer that does not yield within ten seconds will be forced to yield and that coroutine killed. Obviously, in the case of a crash, your code can be interrupted at any point whatsoever.
Draradech #11
Posted 16 January 2013 - 09:58 PM
Thank you! Will turtle movements yield before or after actually moving (or even more general: will they yield before or after an action)? And will writing to and closing files yield? If the turtle yields only before moving, and file write and close doesn't yield, then and only then is absolutely restart-safe turtle position tracking without gps possible…
theoriginalbit #12
Posted 16 January 2013 - 10:24 PM
If the turtle yields only before moving, and file write and close doesn't yield, then and only then is absolutely restart-safe turtle position tracking without gps possible…
Unless the instance crashes… then its stopped wherever it is…
Lyqyd #13
Posted 17 January 2013 - 03:34 AM
Thank you! Will turtle movements yield before or after actually moving (or even more general: will they yield before or after an action)? And will writing to and closing files yield? If the turtle yields only before moving, and file write and close doesn't yield, then and only then is absolutely restart-safe turtle position tracking without gps possible…

Writing files currently does not yield. Turtle movements yield immediately after the instruction is passed to the Java side of things; it then yields to await an event with the result of the movement. There are a number of edge cases wherein turtle tracking can still break down, so if 100% accuracy is required, you will want some manner of correctional update to ensure that the position is accurate.