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

Handle unprotected access to sending state in Emitter from concurrent threads (close #774) #780

Merged
merged 3 commits into from
May 25, 2023

Conversation

matus-tomlein
Copy link
Contributor

Addresses issue #774 that reported a problem with concurrent access to the isSending property in Emitter which wasn't thread-safe.

To address the problem, this PR creates a wrapper class SendingLock around the isSending property that protects access to the value using a lock.

@hakonk
Copy link

hakonk commented May 19, 2023

Thanks for addressing the issue.

I like that the API surface has been reduced in size by making more methods private 👍.

As far as I can see, this makes access to isSending thread safe. That said, pausedEmit is not protected, because it can be accessed both outside and inside the lock of Emitter:334-342:

func resumeEmit() {
    pausedEmit = false
    flush()
}

/// Suspends sending events to collector.
func pauseEmit() {
    pausedEmit = true
}

and Emitter:374-383:

private func sendGuard() {
    if sendingLock.startSendingIfNotSending() {
        objc_sync_enter(self)
        if !pausedEmit {
            attemptEmit()
        }
        objc_sync_exit(self)
        sendingLock.stopSending()
    }
}

I have not managed to create a data race with the minimal example shown in #774, but both resumeEmit and pauseEmit are called from different places in the Snowplow library, so it seems it is possible.

Are there tests in place that assert that Emitter.flush still works as intended?

Copy link

@hakonk hakonk left a comment

Choose a reason for hiding this comment

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

LGTM!

@matus-tomlein
Copy link
Contributor Author

Thank you for reviewing this @hakonk, very useful comments!

@matus-tomlein matus-tomlein merged commit 2199537 into release/5.2.0 May 25, 2023
@matus-tomlein matus-tomlein deleted the issue/774-race_condition branch May 25, 2023 16:13
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.

2 participants