Force a global reset



  • Is there a way to force a global reset from within the game loop with the iVM architecture?

    I tried changing the name of module.exports.loop to module.exports.foo, hoping that would force me back into the pre-loop execution-style for a tick, but it didn't work.

    👍

  • Culture

    I would like this functionality as well, as I can think of a number of cases where it would be useful.



  • If I remember correctly, you can do a partial reset by calling global.requireCache = {}; which will cause all of your modules to get reloaded. This doesn't explicitly reload anything you placed directly in global, but it was good enough for my use-case.



  • infinite loop crunching random numbers ftw...



  • @mrfaul timing out actually doesn't reset the global



  • @gankdalf Thanks, I'll try that. I think I have a memory leak in a closure somewhere. But if I make a code change, everything starts working again just find, so reloading the modules should stop the leak from killing me until I can plug it.

    In keeping with the infinite loop idea...I think if you hit the memory cap it resets the global. So maybe if you just wrote 500 MB to memory in one tick it would work.



  • const heap_bomb = () => { new Uint32Array(8*1024*1024); heap_bomb(); };
    heap_bomb(); 
    

    I've tried this code -- works fine, but takes 2 ticks to reset global. As I see, for now the only way to force global reset is "heap overflow" or critical error, but one does not simply throw a critical error from sandbox ☝

    UPD: well, nope, it takes ~10 ticks, jeez 😅 (at least on PTR):

    [3:38:10 PM][shard0]GAME TIME 24980814
    const heap_bomb = () => { new Uint32Array(8*1024*1024); heap_bomb(); }; heap_bomb();
    [3:38:25 PM][shard0]Script execution timed out ungracefully
    [3:38:32 PM][shard0]Script execution timed out ungracefully, restarting virtual machine
    [3:38:35 PM][shard0]GAME TIME 24980823 
    


  • A code push also triggers a new reset iirc.



  • @joykill, you cannot push code from you code, obviously) See the topic issue:

    Is there a way to force a global reset from within the game loop with the iVM architecture?



  • So after trying a few ways, I created a module that requires itself. I include that conditionally at the end of the loop, and then I can trigger it by conditionally requiring the module without burning CPU or losing a tick.

    Not exactly elegant, but it works.

    👍🤟

  • Culture

    @black0441 does Memory get saved for you or are you losing it on the ticks where you trigger this? Are you getting an error when you call this?



  • Memory is not affected, it's the same thing as a syntax error in a code download.



  • @mototroller said in Force a global reset:

    const heap_bomb = () => { new Uint32Array(810241024); heap_bomb(); };

    This works in console, but doesn't seem to work in code.

    I'm going to test black's solution. Seems like it should work more reliably.



  • @tigga Are you using TS or some sort of code optimiser? This is not work for me because TS is remove allocation of memory when compiling because this variable is never use.



  • No, the code runs, it just causes my script to timeout rather than blowing out the heap and triggering a reset.



  • A syntax error doesn't work, nor did the circular require.

    Still have no way to do this from code AFAIK.



  • How about this?

    let reset = false

    exports.forceReset() = function() { reset = true }

    module.__initGlobals = function() { if (!reset ) return reset = false throw new Error("boom") }

    This will throw an exception outside of the regular evalCode wrapper. I think this will make the vm think it threw the exception and reset the global.