Skip to content

issue #318: Address warning reported by thread safety analyzer #319

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

Merged
merged 1 commit into from
Nov 27, 2020

Conversation

ya-pulser
Copy link
Contributor

Motivation:

Thread safety analyzer reports warning about observable state
leaving lock guarded code blocks. This PR address the warnings.

Analysis of the warning does not prove that we have a real bug and all the
warnings are considered as potential bugs.

Analyzer used is:
docker run --rm -v $(pwd):/src -w /src swift:5.3.1 swift test -c release --sanitize=thread --enable-test-discovery

Modifications:

  • accessor to count of connections in connection pool is guarded by lock
  • forced materialization of a potentially lazy collection into an array before returning
    the collection from lock guarded code block

Result:

Most of thread safety warnings are addressed without modification of observable
code behaviour.

@ya-pulser
Copy link
Contributor Author

@Lukasa Could you please review and guide. I am not sure that we can have tests to accompany the change and I am not sure that I can fully defend that Array(Values) change.

@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Nov 25, 2020
Copy link
Collaborator

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

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

Agree that testing this is very hard. The best test I can come up with is to spin off several dispatch queues and rapidly attempt to mutate the providers in one thread while reading from another. This will catch the bug deterministically under TSAN.

// we materialize Values into an Array before returning from under a lock
// as thread safety analyzer considers safety of Values as undefined and dependant on implementation
// e.g. a lazy collection implementation could by thread unsafe to be returned from within a lock
Array(self.providers.values)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not believe this fix is necessary: I think TSAN is wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am lacking deep knowledge of Swift runtime / libraries here :(.

Is it possible that @ktoso could advise here?

I seen in person issues with returning a lazy collection out of lock guarded block in Scala many years ago and performing of "materialise" was a go-to tool then.

Copy link
Collaborator

Choose a reason for hiding this comment

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

While the dictionary values view is "lazy", conceptually, it is nonetheless still a value type with value semantics. That means that changes to the dictionary to which it points must not manifest in changes to this value. Acquiring the view must be done with a lock, but once it's copied out we're safe. Either TSAN is wrong, or Dictionary in Swift 5.3 is implemented incorrectly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PR is updated to contain only .count fix according to the discussion above.

Motivation:

Thread safety analyzer reports warning about observable state
leaving lock guarded code blocks. This PR address some of the warnings.

Analysis of the warning does not prove that we have a real bug and all the
warnings are considered as potential bugs.

Analyzer used is:
`docker run --rm -v $(pwd):/src -w /src swift:5.3.1 swift test -c
release --sanitize=thread --enable-test-discovery`

Modifications:

* accessor to count of connections in connection pool is guarded by lock

Result:

Most of thread safety warnings are addressed without modification of observable
code behaviour.
@ya-pulser ya-pulser force-pushed the issue_318_cover_with_lock branch from 3e5c3dc to 87b87fa Compare November 27, 2020 17:35
@Lukasa Lukasa merged commit 947429b into swift-server:main Nov 27, 2020
@ya-pulser ya-pulser deleted the issue_318_cover_with_lock branch November 27, 2020 18:01
@artemredkin artemredkin linked an issue Dec 4, 2020 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Access to ConnectionPool.count should be thread safe
2 participants