Skip to content
This repository was archived by the owner on Nov 23, 2021. It is now read-only.

Completion block fix - #49

Closed
carlbrown wants to merge 6 commits into
swift-server:developfrom
carlbrown:completionBlockFix
Closed

Completion block fix#49
carlbrown wants to merge 6 commits into
swift-server:developfrom
carlbrown:completionBlockFix

Conversation

@carlbrown

Copy link
Copy Markdown
Contributor

Fix for completion block issue #36.

Also includes a fix for #48 that caused me to retract my last attempt (PR #47).

Also adds at least one test for #46 while I was at it (but more tests would still be useful there).

func bodyReceived(data: UnsafePointer<Int8>?, length: Int) -> Int32 {
processCurrentCallback(.bodyReceived)
guard let data = data else { return 0 }
if shouldStopProcessingBody {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Optimally, this should be before the assignment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Locking the semaphore to check the Bool value should take precedence over the nil-check?

@seabaylea seabaylea Sep 14, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, I'm getting ahead of myself. I'm not convinced we need any lock protection around a stop flag. We're dealing with two serial queues - one reader and one writer. The only logic that runs on the writer queue is the writeXXXX() calls, so if reads and updates to the stop flag are only by code executing on the reader queue then we don't need a lock.

If we don't need to lock it, it should optimally be before the nil check and assign (unless data = nil is a more common event?)

// just passing in a pointer to the internal ivar. But that ivar can't be modified in
// more than one place, so we have to put a semaphore around it to prevent that.
_shouldStopProcessingBodyLock.wait()
handler(.chunk(data: chunk, finishedProcessing: {self._shouldStopProcessingBodyLock.signal()}), &_shouldStopProcessingBody)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

&_shouldStopProcessingBody isn't protected by the lock....

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we actually need to wait for the async completion handler to run before we can set stop? Don't we want to set it immediately (sync) even if an async writeBody() is still going to happen?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

&_shouldStopProcessingBody isn't protected by the lock....

?
That's what line 326 does.

Do we actually need to wait for the async completion handler to run before we can set stop? Don't we want to set it immediately (sync) even if an async writeBody() is still going to happen

We can't set stop here - it's passed as in inout to the API, and it's set by the handler.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The completion handler that releases the lock runs async (separate thread) - there's no guarantee that its going to call signal() after _shouldStopProcessingBody is modified, so its effectively unprotected.

I was wondering whether your intent was to block access to _shouldStopProcessingBody until after the completion handler has run.

@seabaylea

Copy link
Copy Markdown
Member

Out of interest, is there any reason why stop would be a flag rather than an API call on the HTTPResponseWriter, i.e. alongside done() and abort()?

@carlbrown

Copy link
Copy Markdown
Contributor Author

Out of interest, is there any reason why stop would be a flag rather than an API call on the HTTPResponseWriter, i.e. alongside done() and abort()?

It's not my API, I just implemented it.

@ianpartridge ianpartridge left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please break this down into one PR per problem, or a series of commits that build on each other and can be reviewed independently.

@carlbrown

Copy link
Copy Markdown
Contributor Author

Removing in favor of #51 (and the several others that will be stacked on top of it)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants