Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timers are not canceled after --hot reload #8963

Open
ComputMagnac opened this issue Feb 17, 2024 · 5 comments
Open

Timers are not canceled after --hot reload #8963

ComputMagnac opened this issue Feb 17, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@ComputMagnac
Copy link

ComputMagnac commented Feb 17, 2024

What version of Bun is running?

1.0.25+a8ff7be64

What platform is your computer?

Linux 5.4.0-29-generic x86_64 x86_64

What steps can reproduce the bug?

The error seem to be comming from bun --hot, this error seem to affect the native "setInterval" function.
On hot reload, the setInterval functions seem to be "dismissed", and they keep running like nothing happened.
Here is a simple code example

console.log(`Program started`, Date.now())
setInterval(() => intervalFunction() , 1000); // <-- Works with anonymous functions or simply setInterval(intervalFunction, 1000);

function intervalFunction()
{
    console.log("Hello World !");
}

If you load this program with bun index.js, everything is fine, it will work.
Now, if you run bun --hot index.js, and then save a file inside the hot reload scope, the program will restart, but the previous intervals executions will remain.

2024-02-17.09-54-20.mp4

What is the expected behavior?

Said Up there

What do you see instead?

Said Up there

Additional information

No response

@ComputMagnac ComputMagnac added the bug Something isn't working label Feb 17, 2024
@ComputMagnac ComputMagnac changed the title SetIntervals Executing Multiples Times ? SetIntervals Executing Exponentially with --hot Feb 17, 2024
@Electroid
Copy link
Contributor

We should probably cancel timers on --hot reload.

@Electroid Electroid changed the title SetIntervals Executing Exponentially with --hot Timers are not canceled after --hot reload Feb 17, 2024
@ComputMagnac
Copy link
Author

For anyone using --hot as of today, you can use a simple looping alternative:

console.log(`Program started`, Date.now())
intervalFunction(); 

function intervalFunction()
{
    console.log("Hello World !");
    await new Promise(resolve => setTimeout(resolve, 1000))
    return await intervalFunction();
}

@ComputMagnac
Copy link
Author

Update, it appears that the issue occurs on setInterval.

/*
[DEBUG] Loading new schedule : _schedules/paidProductsDisabler with interval of 1000
[DEBUG] Controller Executed at 1708544326475
[2] Billing status: 1 | Days Left: 17.86 | Failed Days Left: null 
[4] Billing status: 3 | Days Left: 17.87 | Failed Days Left: null 
[DEBUG] Controller Executed at 1708544326479
[2] Billing status: 1 | Days Left: 17.86 | Failed Days Left: null 
[4] Billing status: 3 | Days Left: 17.87 | Failed Days Left: null 
[DEBUG] Controller Executed at 1708544326482
[2] Billing status: 1 | Days Left: 17.86 | Failed Days Left: null 
[4] Billing status: 3 | Days Left: 17.87 | Failed Days Left: null 
[DEBUG] Controller Executed at 1708544326485
[2] Billing status: 1 | Days Left: 17.86 | Failed Days Left: null 
[4] Billing status: 3 | Days Left: 17.87 | Failed Days Left: null 
One is supposed to arrive
*/

console.log(`Entering loading Schedules [Total: ${this.#schedules.length} schedules]`);
for (let schedule of this.#schedules)
{
    console.log(`[DEBUG] Loading new schedule : ${schedule.controller} with interval of ${schedule.interval}`);
    setTimeout(() => {
        let res = new ResponseBuilder(this.#directory, "");
        return res.loadController(schedule.controller, { 
            models: this.#models
        }, res)
    }, schedule.interval);
}

@anilcan-kara
Copy link

For anyone using --hot as of today, you can use a simple looping alternative:

console.log(`Program started`, Date.now())
intervalFunction(); 

function intervalFunction()
{
    console.log("Hello World !");
    await new Promise(resolve => setTimeout(resolve, 1000))
    return await intervalFunction();
}

Thank you,

still there are some native or npm packages using interval e.g. @elysiajs/cron

@ComputMagnac
Copy link
Author

Well, the objective would be to resolve the conflicts linked to Bun --hot and Timers, I do not see how using cron resolves the issue by itself as it is a core Javascript feature that is "compromised"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants