Skip to content

Always access Task’s connection and cancelled property through a lock #385

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

Closed
wants to merge 2 commits into from

Conversation

fabianfett
Copy link
Member

Motivation

  • The task's properties connection and cancelled are sometime accessed through a lock and sometimes not.

Modifications

  • Let's always go through a lock

@fabianfett fabianfett requested a review from artemredkin June 29, 2021 07:43
@fabianfett fabianfett force-pushed the ff-make-thread-safe branch from e226032 to aff64fd Compare June 29, 2021 07:44
@fabianfett
Copy link
Member Author

Failure in known flaky test #371. @swift-server-bot test this please

let requestFuture = httpClient.execute(request: request, delegate: delegate).futureResult
let logger = Logger(label: "test-connection")

let clientFactory = HTTPConnectionPool.ConnectionFactory(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why has this dropped down to be so low-level?

Copy link
Member Author

Choose a reason for hiding this comment

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

I would really like to not expose the connection on the task in the future. In order to have control over the channel, we need to create it ourselves here.

Copy link
Member Author

Choose a reason for hiding this comment

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

There is only one other test remaining that accesses the task.connection:

testWeCanActuallyExactlySetTheEventLoops

We should be able to remove task.connection from testWeCanActuallyExactlySetTheEventLoops, once the new connection pool has landed. We would test only the delegate calls, since all connection eventLoop requirements can be tested with unit tests and will be tested with unit tests.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, can we encapsulate this new code in a function that clearly explains what it does? The code here is very aware of implementation details of AHC, which suggests the test as a whole needs rewriting, but in the near term I'd be ok with just hiding the implementation details in a function.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is still open.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Bump. 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, ignored it for now, since I think we should be able to just remove this test, once other PRs have landed. The functionality will be tested by unit tests.

self.lock.withLock { self._cancelled }
}

var connection: Connection? {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems like this is only used in tests: can we mark it clearly for that purpose?

Copy link
Member Author

Choose a reason for hiding this comment

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

Will do!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, I wasn't clear: I meant changing the name to var _testOnly_connection

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is used on L715, maybe make it private and add an internal _testOnly_connection per Cory's suggestion?

@@ -702,7 +713,9 @@ extension HTTPClient {

func fail<Delegate: HTTPClientResponseDelegate>(with error: Error,
delegateType: Delegate.Type) {
if let connection = self.connection {
let maybeConnection = self.lock.withLock { self._connection }
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: why not use self.connection here?

@@ -681,13 +689,16 @@ extension HTTPClient {

@discardableResult
func setConnection(_ connection: Connection) -> Connection {
Copy link
Collaborator

Choose a reason for hiding this comment

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

not related to this PR, but why do we also return the connection that we set here?

@fabianfett fabianfett added this to the HTTP/2 support milestone Jun 29, 2021
@fabianfett fabianfett requested review from Lukasa and glbrntt June 29, 2021 12:03
@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Jul 2, 2021
@fabianfett fabianfett closed this Sep 20, 2021
@fabianfett fabianfett deleted the ff-make-thread-safe branch April 12, 2022 06:42
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.

5 participants