Skip to content

Fix node timers #619

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Fix node timers #619

wants to merge 5 commits into from

Conversation

conico974
Copy link
Collaborator

It is expected that the global timers (i.e. setInterval, setTimeout) in Node returns a Nodejs.Timeout instead of a number. On cloudflare workers they return a number.

This can cause some unexpected crash like in #587, where sentry tries to call unref() on the created interval.
This PR fix #587

The only downsides to doing that is that in the middleware we'll have access to the Node version as well, but i don't think this is too big of an issue

Copy link

changeset-bot bot commented Apr 30, 2025

🦋 Changeset detected

Latest commit: 507ca50

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@opennextjs/cloudflare Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

pkg-pr-new bot commented Apr 30, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/cloudflare@619

commit: 507ca50

@vicb
Copy link
Contributor

vicb commented Apr 30, 2025

Thanks for the fix @conico974

FYI this was discussed internally and we decided that mixing APIs would not be a problem (documented here).

I'll raise the issue again internally.

One possible fix would be to update unenv (with the caveat that external modules would not be polyfilled).

For now I'm +1 for this PR.

@@ -144,6 +144,10 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
// This define should be safe to use for Next 14.2+, earlier versions (13.5 and less) will cause trouble
"process.env.__NEXT_EXPERIMENTAL_REACT": `${needsExperimentalReact(nextConfig)}`,
},
banner: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move the code to init.ts#initRuntime

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already tried that, putting the import there does not work.
And as said in the comment we can't just assign to globalThis without causing a Maximum call stack error.
We could also polyfill it in initRuntime, which is what i initially did in d7ce16b

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I miss the comment...

Would Object.assign() work instead of direct assignment?

There might be some interaction with unenv, I can take a look at that

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already tried that as well.
I think that the current implementation in node:timers internally use globalThis.setInterval which make it call itself

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Adding Sentry causes app to crash in preview mode
2 participants