Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

[node] prevent race condition for renderFinished - #11948

Merged
kkaefer merged 1 commit into
masterfrom
node-renderfinished-race-condition
May 22, 2018
Merged

kkaefer merged 1 commit into
masterfrom
node-renderfinished-race-condition

Conversation

@kkaefer

@kkaefer kkaefer commented May 18, 2018 •

Copy link
Copy Markdown
Member

This happens when the render finished on the thread, and the user cancels a request in the same event loop iteration. There's an assert in Nan that catches this for Debug builds, but won't prevent it for release builds.

In addition to possibly invoking undefined behavior when we try to call an empty callback, or try to access empty image data, there's a heinous memory leak. Here's what happens when we call Unref() twice as part of renderFinished:

refcount changes during a render with the above mentioned race condition that leads to renderFinished being called twice:

    initial: 0
    Ref() -> 1, calls ClearWeak()
    Unref() -> 0, calls MakeWeak()
    Unref() -> -1

next render:

    initial: -1 (!)
    Ref() -> 0, calls ClearWeak()
    Unref() -> -1   ==> MakeWeak() is not called!

From now on, this Map object can never be garbage collected, since the refcount is negative, which means that MakeWeak() will never be called as part of Unref().

@friedbunny friedbunny added bug Core The cross-platform C++ core, aka mbgl labels May 18, 2018
ian29 added a commit that referenced this pull request May 21, 2018

@brunoabinader brunoabinader left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm, so from my understanding

does not guarantee that the callback is valid during renderFinished() scope, and we currently happen to unref() before the check.

@kkaefer

kkaefer commented May 22, 2018

Copy link
Copy Markdown
Member Author

does not guarantee that the callback is valid

You're right. However, that constraint is now enforced as part of this PR, which checks callback and aborts if it's empty.

@kkaefer
kkaefer merged commit d5aff71 into master May 22, 2018
@kkaefer
kkaefer deleted the node-renderfinished-race-condition branch May 22, 2018 09:02
@kkaefer kkaefer mentioned this pull request May 22, 2018
kkaefer added a commit that referenced this pull request Jul 12, 2018
…der is canceled

Reverts d5aff71 (#11948) and implements an alternative solution.

Even with the previous fix applied, we were calling `renderFinished()` directly when the render was canceled. This led to the following scenario (all on the main thread):
- `Map::renderStill()` callback is called, `uv_async_send` schedules a call to `renderFinished()`.
- `NodeMap::cancel()` destroys the map object, and calls `renderFinished()` directly, which cleans up the request
- `NodeMap::startRender()` is called to schedule a _new_ render, which means that a new callback will be set
- The `uv_async_t` fires and calls `renderFinished` again:
   - We don't return early as intended by the fix in #11948 since a new callback/`req` object is now available.
   - Since neither error nor image has been set, we'll see a `Didn't get an image` error message.
   - We call the callback with the error. In scenario where we're reusing Map objects, we will start a new render, which will now trigger the `Map is currently rendering an image` error in `Map::renderStill`, and the map object enters a circle in which it will now fail repeatedly with this error.

This commit fixes the issue by completely removing the existing `uv_async_t` handle that could have a scheduled call, and replacing it with a fresh instance of an `uv_async_t` handle.
kkaefer added a commit that referenced this pull request Jul 13, 2018
…der is canceled

Reverts d5aff71 (#11948) and implements an alternative solution.

Even with the previous fix applied, we were calling `renderFinished()` directly when the render was canceled. This led to the following scenario (all on the main thread):
- `Map::renderStill()` callback is called, `uv_async_send` schedules a call to `renderFinished()`.
- `NodeMap::cancel()` destroys the map object, and calls `renderFinished()` directly, which cleans up the request
- `NodeMap::startRender()` is called to schedule a _new_ render, which means that a new callback will be set
- The `uv_async_t` fires and calls `renderFinished` again:
   - We don't return early as intended by the fix in #11948 since a new callback/`req` object is now available.
   - Since neither error nor image has been set, we'll see a `Didn't get an image` error message.
   - We call the callback with the error. In scenario where we're reusing Map objects, we will start a new render, which will now trigger the `Map is currently rendering an image` error in `Map::renderStill`, and the map object enters a circle in which it will now fail repeatedly with this error.

This commit fixes the issue by completely removing the existing `uv_async_t` handle that could have a scheduled call, and replacing it with a fresh instance of an `uv_async_t` handle.
kkaefer added a commit that referenced this pull request Jul 13, 2018
…der is canceled

Reverts d5aff71 (#11948) and implements an alternative solution.

Even with the previous fix applied, we were calling `renderFinished()` directly when the render was canceled. This led to the following scenario (all on the main thread):
- `Map::renderStill()` callback is called, `uv_async_send` schedules a call to `renderFinished()`.
- `NodeMap::cancel()` destroys the map object, and calls `renderFinished()` directly, which cleans up the request
- `NodeMap::startRender()` is called to schedule a _new_ render, which means that a new callback will be set
- The `uv_async_t` fires and calls `renderFinished` again:
   - We don't return early as intended by the fix in #11948 since a new callback/`req` object is now available.
   - Since neither error nor image has been set, we'll see a `Didn't get an image` error message.
   - We call the callback with the error. In scenario where we're reusing Map objects, we will start a new render, which will now trigger the `Map is currently rendering an image` error in `Map::renderStill`, and the map object enters a circle in which it will now fail repeatedly with this error.

This commit fixes the issue by completely removing the existing `uv_async_t` handle that could have a scheduled call, and replacing it with a fresh instance of an `uv_async_t` handle.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug Core The cross-platform C++ core, aka mbgl

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants