Skip to content

Conversation

@sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Jul 15, 2022

Supersedes #17438 and #17376.

@nokotan
Copy link
Contributor

nokotan commented Jul 16, 2022

LGTM. The approach is different from #17376, #17376 can be closed.

@sbc100 sbc100 force-pushed the simplify_callUserCallback branch from e5cba5a to 584a0bf Compare July 18, 2022 22:39
@sbc100 sbc100 force-pushed the fix_glfw branch 2 times, most recently from f6f23dd to fda49b7 Compare July 18, 2022 22:41
@sbc100 sbc100 changed the base branch from simplify_callUserCallback to main July 18, 2022 22:41
src/library.js Outdated
#if ASSERTIONS
assert(!inUserCode, 'callUserCallback called when already running user code');
inUserCode = true;
#endif
Copy link
Member

Choose a reason for hiding this comment

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

Why is this an error?

Copy link
Collaborator Author

@sbc100 sbc100 Jul 18, 2022

Choose a reason for hiding this comment

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

The idea is that callUserCallback is only ever called from the event loop when entering user code.

This is important because callUserCallback take care of shutting down the runtime after the user code is done. If we allow callUserCallback to be called while we are already inside of user code then it could exit while running that user code.

Sadly, my initial is of doing runtimeKeepalivePush() before calling main doesn't work because we actually do want to be able to exit the runtime (i.e. call exit()) when running user code. runtimeKeepalivePush() means that something other than the current callstack is keeping the runtime alive. So keepRuntimeAlive() need to return false during main() (or during any callback).

Another way of putting it: callUserCallback is assuming that when the user code is done it is returning to the event loop (and its safe moment to shut down the runtime) .

Copy link
Member

Choose a reason for hiding this comment

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

Is that behavior of callUserCallback documented anywhere? I had never thought of it like that, fwiw... I thought it could be used also not from the event loop, like directly in running code, and I think we have such uses of it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we did have such uses, then they would show up as test failures here, right?

If you are already in a user stack then you can just invoke the callback yourself.. what would be the point of using callUserCallback in that case? You wouldn't want it to do any of the things it normally does... which is specifically why @dschuff added the, now-removed, syncronous argument.. which bypassed what calllUserCallback was actually doing.

Anyone assuming they can use callUserCallback when they are already in a user stack would already be broken, as in this glfw bug.

Copy link
Collaborator Author

@sbc100 sbc100 Jul 19, 2022

Choose a reason for hiding this comment

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

The first line of the docs seem pretty clear to me: Used to call user callbacks from the embedder / event loop.

If you are not coming from the event loop you simply don't need to call this function.

This new assertion just helps such users know that they are making a mistake.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How about if I made this into a warnOnce rather than an assertion? I think that might be a better way to go.

@sbc100 sbc100 force-pushed the fix_glfw branch 2 times, most recently from a5aace4 to 7f9ec3b Compare July 19, 2022 00:41
sbc100 added a commit that referenced this pull request Jul 19, 2022
Detect cases where `callUserCallback` is called while already running
user code.  This doesn't change the behaviour, but warns about
(currently) incorrect usage of `callUserCallback`.

Split out from #17441 which fixes a real bug in glfw where
`callUserCallback` could cause the runtime to exit mid-program.

This bug only effects programs that are built with `EXIT_RUNTIME` but
its serious enough we probably want to look at more robust solution.

For now, issuing a warning seems like a good first step.
sbc100 added a commit that referenced this pull request Jul 19, 2022
Detect cases where `callUserCallback` is called while already running
user code.  This doesn't change the behaviour, but warns about
(currently) incorrect usage of `callUserCallback`.

Split out from #17441 which fixes a real bug in glfw where
`callUserCallback` could cause the runtime to exit mid-program.

This bug only effects programs that are built with `EXIT_RUNTIME` but
its serious enough we probably want to look at more robust solution.

For now, issuing a warning seems like a good first step.
sbc100 added a commit that referenced this pull request Jul 19, 2022
Detect cases where `callUserCallback` is called while already running
user code.  This doesn't change the behaviour, but warns about
(currently) incorrect usage of `callUserCallback`.

Split out from #17441 which fixes a real bug in glfw where
`callUserCallback` could cause the runtime to exit mid-program.

This bug only effects programs that are built with `EXIT_RUNTIME` but
its serious enough we probably want to look at more robust solution.

For now, issuing a warning seems like a good first step.
sbc100 added a commit that referenced this pull request Jul 19, 2022
Detect cases where `callUserCallback` is called while already running
user code.  This doesn't change the behaviour, but warns about
(currently) incorrect usage of `callUserCallback`.

Split out from #17441 which fixes a real bug in glfw where
`callUserCallback` could cause the runtime to exit mid-program.

This bug only effects programs that are built with `EXIT_RUNTIME` but
its serious enough we probably want to look at more robust solution.

For now, issuing a warning seems like a good first step.
@sbc100 sbc100 changed the title Fix glfw usage of callUserCallback and add checks around it Fix glfw usage of callUserCallback Jul 21, 2022
@sbc100
Copy link
Collaborator Author

sbc100 commented Jul 21, 2022

I reverted all the code to check for misused of callUserCallback. We can leave futher discussion of that to #17476.

That should make this change a lot less controversial as it simply fixes a bug.

@sbc100 sbc100 requested a review from kripken July 21, 2022 22:32
@sbc100 sbc100 enabled auto-merge (squash) July 21, 2022 23:06
@sbc100 sbc100 merged commit e17c7f2 into main Jul 21, 2022
@sbc100 sbc100 deleted the fix_glfw branch July 21, 2022 23:45
sbc100 added a commit that referenced this pull request Jan 3, 2025
Detect cases where `callUserCallback` is called while already running
user code.  This doesn't change the behaviour, but warns about
(currently) incorrect usage of `callUserCallback`.

Split out from #17441 which fixes a real bug in glfw where
`callUserCallback` could cause the runtime to exit mid-program.

This bug only effects programs that are built with `EXIT_RUNTIME` but
its serious enough we probably want to look at more robust solution.

For now, issuing a warning seems like a good first step.
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.

4 participants