What actually happens in a script when the sim restarts?

Creating scripts
User avatar
Snoots Dwagon
Posts: 422
Joined: Mon Jul 30, 2018 9:45 pm
Has thanked: 442 times
Been thanked: 779 times

Re: What actually happens in a script when the sim restarts?

Post by Snoots Dwagon »

John, I've verified what you state above. I went to my own server on OSgrid and created a prim with the following script:

Code: Select all

integer count;

default{

touch_start(integer a){
count++;
llOwnerSay("Count="+(string) count);
}
}
Touching the prim 7 times, it counted to 7.

I then logged out and reset the Opensim instance (told it to shut down, which it did. Program wiped from memory.)

I gave it a bit to sit then re-started the region. Once it re-started I logged back in and touched the prim.

8. 9. 10.

So yes, the system DOES retain script data when the region is entirely shut down. I have to admit that is totally new information to me... because I've seen so many scripts lose their state and data on a sim reset. As far as I know those scripts were well-written, so I'm wondering if it's a random bug somewhere within LsL or the region server code. More research is required... but I'm behind on my Netflix binging. ;D
These users thanked the author Snoots Dwagon for the post:
Dale Innis
~~~~~~~
I'm a dwagon in real life too. (Ask my sister, who totally agrees.)

~~~~~~~
User avatar
Dale Innis
Posts: 41
Joined: Thu Mar 13, 2014 1:59 am
Has thanked: 84 times
Been thanked: 49 times

Re: What actually happens in a script when the sim restarts?

Post by Dale Innis »

Thanks for all of this! That pretty much agrees with my own experience.

To make my specific remaining question more concrete, consider this (very artificial) example:

Code: Select all

default {
 
state_entry() {
  llSetTimerEvent(30.0 + llFrand(30.0));  // Wait for 30-60 seconds
}

timer() {
  llSetTimerEvent(0.0);  // Turn off timer for now
  integer i;
  for (i=0; i < 20; i++) {  // Count up to 20, slowly
    llSay(0, (string)i);  
    llSleep(1.0);
  }
  llSetTimerEvent(30.0 + llFrand(30.0));  // Wait for 30-60 seconds
}

}  // default
The thing that I don't know is what happens if the sim is shut down during the roughly 20 seconds that the script spends in the timer() event, counting slowly up to 20 (with one-second sleeps in between). Obviously this isn't something that it makes much sense to do :) but the general question is "what happens if the sim shuts down while in an event handler?".

In this case, let's say the sim shuts down when the script is sleeping after saying "11". Later it's restarted, so what happens?

The four possibilities that I can think of are:

1) The sim will in fact not shut down until all scripts have either returned from the current handler, or gotten reset due to the event time limit (which is I think 30 seconds); in this case the sim will keep running for another 9 seconds, the script will count all the way to 19 and then set a new timer event value and return from the handler. Then the sim would actually shut down, and then it's restarted the script will wait a few seconds and then start counting from 0 again.

2) The entire state of the script, including exactly what it was doing and what all values were, is saved and restored. In this case, it will say "11", the sim will be shut down, and then when the sim comes back up again it will say "12", "13", etc.

3) The fact that it was in the handler is saved and restored, but not where it was. In this case, when the sim is restarted it will start back at saying "0" again, count up to "19", set a new timer event, and all will be well.

4) The fact that it was in the handler isn't saved and restored, in which case since there was no timer event currently scheduled, once the sim is started back up the script will never say anything again, until something else (probably manual intervention) resets the script (losing any internal data, etc).

I have seen a few things that make me think that the case might be (4), but I'd love to see the actual code.

(If the case is (4), this should be made clear to people somewhere, and the right fix is just not to do the llSetTimerEvent(0.0) at the start of the timer() handler in this case, and generally to make sure that if a handler exits unexpectedly, other handlers will eventually fire and get things going again.)

Hope that all makes sense! I can't think of a great way to test this offhand, as it would be hard to get all the timings and observations just right.
User avatar
John Mela
Posts: 91
Joined: Tue Feb 04, 2014 9:50 pm
Has thanked: 139 times
Been thanked: 127 times
Contact:

Re: What actually happens in a script when the sim restarts?

Post by John Mela »

I don't know what happens to events in progress at the time the sim stopped. I would expect they'd be terminated.

A good practice is to design your code for cases where events fail to execute. I've been trying to do this more lately, in the knowledge that OpenSim will terminate events if they've been running too long. Bear in mind that heavy system load can cause this to happen even if an event normally executes in a reasonable time under normal load. Another possibility is that events may not fire if the events queue becomes full - again, this can happen when CPU is loaded even if testing under normal conditions is fine.
These users thanked the author John Mela for the post (total 2):
Snoots DwagonDale Innis
User avatar
Snoots Dwagon
Posts: 422
Joined: Mon Jul 30, 2018 9:45 pm
Has thanked: 442 times
Been thanked: 779 times

Re: What actually happens in a script when the sim restarts?

Post by Snoots Dwagon »

To back up what John is saying, there's a forum (fairly recent) which discusses event time limits. It was revealed (I think by John, not sure) that on Kitely (and as an Opensim default) events are set to time out at 30 seconds. Unfortunately when this happens, it is possible for the entire script to shut down rather than simply exiting the event. This evidently was originally intended to prevent a script from negatively impacting region performance, but in my dwagony opinion, it does more harm than good. There are sensible instances in which one may enter an event and remain there for some time. One example would be a listen event which in turn calls a lengthy subroutine. Shutting down the script just because it has been in the listen event for 30 seconds seems counter-productive... and alternative ways of writing such a script are cumbersome.

So without testing this on my local server, I would be with John in thinking that in most cases the event would "time out" long before the region re-started. That would depend on whether events are timed by system clock or operational time (from the structure of LsL I'd be quick to believe they're timed by system time). In any case, I'd wager the odds of an event shutting down because of the 30 second time limit to be far greater than because a world is shut down. If that happens, chances are the entire script will cease to function and need re-starting.

Dumb ways to die... so many dumb ways to die...
https://youtu.be/IJNR2EpS0jw


A world shutdown isn't instantaneous and is orderly, so the chances of being inside an event when it does so is unlikely. (Just pot-shotting here. No real data to verify this statement.) If a region crashes suddenly then certainly an event may be impacted... which may (now that I think of it) be one reason some vending scripts go to an offline state after a reset: the sim crashed, the script died from an event timeout, and requires the owner to set money permissions on it again. Again, no way of knowing for sure without extensive and time-consuming tests.
These users thanked the author Snoots Dwagon for the post:
Dale Innis
~~~~~~~
I'm a dwagon in real life too. (Ask my sister, who totally agrees.)

~~~~~~~
User avatar
Dale Innis
Posts: 41
Joined: Thu Mar 13, 2014 1:59 am
Has thanked: 84 times
Been thanked: 49 times

Re: What actually happens in a script when the sim restarts?

Post by Dale Innis »

Thanks to all! So that's my case (4), and I do agree that seems to be the thing that happens.

On the 30-second limit: that was probably me :) investigating some similar things. It seems that if a handler doesn't exit for 30 seconds, the entire script gets reset. Which is mostly fine with me, actually, in the large, as my scripts are okay with being randomly reset. (It's only a problem in that whatever the handler was supposed to do will never get done.)

That thread: viewtopic.php?f=26&t=5628

It's a good point that one would like to be robust against a handler just not being called at all. It's not clear exactly how to do that, though, without imposing too much of a burden on the sim. I guess a timer could be backed up by a repeating sensor that senses at long intervals for something that's never going to happen, and then use no_sensor() to check that the timer's still firing, and vice-versa. But I worry about the resource impacts of having lots of scripts doing that.

To start with, at least, I'm going to be auditing my important scripts for "what would go wrong if this handler were to just exit halfway through, and how can I mitigate it?".
User avatar
Krull Kitty
Posts: 100
Joined: Thu Aug 06, 2020 1:25 pm
Has thanked: 39 times
Been thanked: 81 times

Re: What actually happens in a script when the sim restarts?

Post by Krull Kitty »

Ilan Tochner wrote:
Tue Dec 29, 2020 5:05 pm
Scripts are stopped whenever the world is stopped. I recommend you add checks for the region start event so that active scripts will be able to resume working after a world restart. See: http://wiki.secondlife.com/wiki/CHANGED_REGION_START
Ooooooh thats a nifty idea :D

Have to play with that function soon :)
Post Reply