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

Don't completely crash server on uncaught exception #228

Merged
merged 7 commits into from
May 11, 2024

Conversation

alex-anthropic
Copy link
Contributor

Partial mitigation for #225

This doesn't fix the underlying AttributeError: 'WSStream' object has no attribute 'connection' but it does allow the server to stay up and healthy when it encounters this bug.

@@ -48,28 +49,31 @@ async def run(self) -> None:
socket = self.stream.socket
ssl = False

def log_handler(e: Exception) -> None:
if self.config.log.error_logger is not None:
self.config.log.error_logger.exception("Internal hypercorn error")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you want error_logger.error("Internal hypercorn error", exc_info=e), because the exception being passed in might only be a slice of the exception group that was being caught, so the implicit "pull the current exception from ambient context" logic in error_logger.exception might not grab the right data

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good call, done!

await self._start_idle()
await self._read_data()
except OSError:
pass
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's a minor behavioral change here where we used to not log OSErrors, but now we do. Probably it's better to keep that, because OSErrors are probably like "the other side hung up" or similar and not interesting? I think you can do it with like

catch({OSError: ignore_handler, Exception: log_handler})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@njsmith njsmith merged commit bb34f36 into pgjones:main May 11, 2024
13 checks passed
@njsmith njsmith deleted the alex/dont_crash_on_uncaught branch May 11, 2024 01:48
pgjones added a commit that referenced this pull request May 25, 2024
I wish to consider this solution, and also ensure any solution applies
in the same manner for asyncio and UDP servers.
@njsmith
Copy link
Collaborator

njsmith commented Sep 23, 2024

@pgjones we just discovered the hard way that you reverted this... did you come up with the better solution you were looking for? Or could we un-revert it until you have a chance to replace with the proper fix?

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