Skip to content

Fix request error handling#71

Merged
aryan-25 merged 3 commits intoswift-server:mainfrom
aryan-25:fix-request-error-handling
Mar 27, 2026
Merged

Fix request error handling#71
aryan-25 merged 3 commits intoswift-server:mainfrom
aryan-25:fix-request-error-handling

Conversation

@aryan-25
Copy link
Copy Markdown
Collaborator

Motivation:

We currently propagate errors out of the handleRequestChannel method.

For the plaintext HTTP/1.1 transport, the handleRequestChannel method is called from within a withThrowingDiscardingTaskGroup without any error catching.

Since withThrowingDiscardingTaskGroup cancels itself whenever any of the tasks spawned inside of it throws, an error thrown in one connection brings the entire server down. We should fix this.

Modifications:

  • Modified the behaviour of handleRequestChannel to close the channel when an error is caught instead of propagating the error upwards.
  • Added an associated test case.

Result:

An error thrown in a plaintext HTTP/1.1 connection no longer results in the parent task group being cancelled.

@aryan-25 aryan-25 added the 🔨 semver/patch No public API change. label Mar 27, 2026
Copy link
Copy Markdown
Collaborator

@gjcairo gjcairo left a comment

Choose a reason for hiding this comment

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

Thanks Aryan!

@aryan-25 aryan-25 merged commit 13b0f5b into swift-server:main Mar 27, 2026
25 of 26 checks passed
@aryan-25 aryan-25 deleted the fix-request-error-handling branch March 27, 2026 21:27
@@ -289,7 +289,7 @@
} catch {
self.logger.debug("Error thrown while handling connection: \(error)")
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.

not a change made in this pr, but we should never use string interpolation for log messages. the correct pattern is to put the error message into metadata.

Suggested change
self.logger.debug("Error thrown while handling connection: \(error)")
self.logger.debug("Error thrown while handling connection", metadata: ["error": "\(error)"])

Copy link
Copy Markdown
Collaborator

@gjcairo gjcairo Apr 1, 2026

Choose a reason for hiding this comment

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

I think this was already changed in a different PR - in any case there's an open issue to audit these log messages.

self.logger.debug("Error thrown while handling connection: \(error)")
// TODO: We need to send a response head here potentially
throw error
try? await channel.channel.close()
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.

what has happened to the OG error? that is just swallowed here? I think we should at least add a comment, why we swallow it here.

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.

also I think, we should log the error, if we can't close the connection.

Copy link
Copy Markdown
Collaborator

@gjcairo gjcairo Apr 1, 2026

Choose a reason for hiding this comment

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

The change in this PR was a bit of a workaround to avoid the whole server coming down when an error is thrown. I will work on handling this properly - that's part of what the TODO right above is. I'll make sure we also log the error when closing.

Comment on lines 218 to 219
handler: some HTTPServerRequestHandler<RequestConcludingReader, ResponseConcludingWriter>
) async throws {
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.

this should remove the throws keyword here, which will make usage of TaskGroups much more explicit.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yeah, this I missed, we should remove it - mind opening a small follow up @aryan-25 ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I'm going to shortly create a PR to address @fabianfett's comments.

@aryan-25 aryan-25 mentioned this pull request Apr 1, 2026
@aryan-25
Copy link
Copy Markdown
Collaborator Author

aryan-25 commented Apr 1, 2026

Feedback from @fabianfett is addressed in #76.

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.

3 participants