-
Notifications
You must be signed in to change notification settings - Fork 179
Enabled logging of failed port bind #489
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -370,8 +370,14 @@ def listen(self, num): | |||||||||
| return self.socket.listen(num) | ||||||||||
|
|
||||||||||
| def bind(self, addr): | ||||||||||
| # self.logger.log(logging.DEBUG, "Attempting to bind to: %s" % addr) | ||||||||||
| self.addr = addr | ||||||||||
| return self.socket.bind(addr) | ||||||||||
| try: | ||||||||||
| return self.socket.bind(addr) | ||||||||||
| except Exception as exc: | ||||||||||
| self.logger.log(logging.CRITICAL, "Failed bind to: %s" % str(addr)) | ||||||||||
| self.logger.log(logging.CRITICAL, "Exception raised: %s" % str(exc)) | ||||||||||
|
Comment on lines
+378
to
+379
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer invoking logging like that as well; I was just going with the existing usage in the rest of the file. Regarding
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A good point. It depends on the address family, so as Waitress supports if self.socket.family == socket.AF_INET:
self.logger.critical("Failed bind to %s:%d: %s", addr[0], addr[1], exc)
elif self.socket.family == socket.AF_INET6:
self.logger.critical("Failed bind to [%s]:%d: %s", addr[0], addr[1], exc)
else:
self.logger.critical("Failed bind to %s: %s", addr, exc)That'll handle IPv4 and IPv6 addresses, I think, though really some kind of proper formatting function would be better. I'll leave it up to you as to whether you just want to format the address with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the ipv4 & unix socket was why I just used a single string representation to start with. i did not think of ipv6, which complicates things even more. I think this might be a good option: The addr will be in the backticks, the exc is after. That should be clear for humans and is easy to read/process by machine. I've got a block of time tomorrow to play with this more. |
||||||||||
| raise | ||||||||||
|
|
||||||||||
| def accept(self): | ||||||||||
| # XXX can return either an address pair or None | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||||||||||||
| import errno | ||||||||||||||||
| import socket | ||||||||||||||||
| import sys | ||||||||||||||||
| import unittest | ||||||||||||||||
|
|
||||||||||||||||
| dummy_app = object() | ||||||||||||||||
|
|
@@ -311,6 +312,42 @@ def test_create_with_one_socket_handle_accept_noerror(self): | |||||||||||||||
| self.assertListEqual(innersock.opts, [("level", "optname", "value")]) | ||||||||||||||||
| self.assertListEqual(L, [(inst, innersock, None, inst.adj)]) | ||||||||||||||||
|
|
||||||||||||||||
| @unittest.skipIf( | ||||||||||||||||
| sys.platform.startswith("win"), "This test is not supported on Windows" | ||||||||||||||||
| ) | ||||||||||||||||
|
Comment on lines
+315
to
+317
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is it breaking on Windows. I see nothing here that shouldn't work. My suggestions might help fix whatever issue was causing issues on Windows, but if they don't, it'd be better to say why it doesn't work on Windows rather than just stating that it doesn't.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I just re-read the description. Maybe something like this would be better:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree |
||||||||||||||||
| def test_port_bind_failure_logging(self): | ||||||||||||||||
| # ensure the address is logged on a failed port bind | ||||||||||||||||
|
|
||||||||||||||||
| # create a first app correctly | ||||||||||||||||
| inst_a = self._makeOne(port=8080) | ||||||||||||||||
|
|
||||||||||||||||
| # Ensure a second app correctly binds to a different host+port | ||||||||||||||||
| inst_b = self._makeOne(port=8081) | ||||||||||||||||
|
Comment on lines
+321
to
+325
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Will do. I'm not very familiar with waitress, and other pyramid projects that use this concept don't have this concern.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||
|
|
||||||||||||||||
| # a third app should fail the bind to the fist app's host+port | ||||||||||||||||
| with self.assertLogs("waitress", level="ERROR") as cm_log: | ||||||||||||||||
| with self.assertRaises(OSError) as cm: | ||||||||||||||||
| inst_c = self._makeOne(port=8080) | ||||||||||||||||
| self.assertTrue( | ||||||||||||||||
| ("[Errno 48] Address already in use" == str(cm.exception)) | ||||||||||||||||
| or ("[Errno 98] Address already in use" == str(cm.exception)) | ||||||||||||||||
| ) | ||||||||||||||||
|
Comment on lines
+331
to
+334
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like this is preferable:
Suggested change
Using the named constants is more cross-platform.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks so much for this. I was going through docs and the matrix trying to figure out what was going on; using errno makes so much more sense. |
||||||||||||||||
|
|
||||||||||||||||
| self.assertIn( | ||||||||||||||||
| "CRITICAL:waitress:Failed bind to: ('127.0.0.1', 8080)", | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on my previous suggestions:
Suggested change
|
||||||||||||||||
| cm_log.output, | ||||||||||||||||
| ) | ||||||||||||||||
| self.assertTrue( | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit brittle. If you want a test like this, you should be using the |
||||||||||||||||
| ( | ||||||||||||||||
| "CRITICAL:waitress:Exception raised: [Errno 48] Address already in use" | ||||||||||||||||
| in cm_log.output | ||||||||||||||||
| ) | ||||||||||||||||
| or ( | ||||||||||||||||
| "CRITICAL:waitress:Exception raised: [Errno 98] Address already in use" | ||||||||||||||||
| in cm_log.output | ||||||||||||||||
| ) | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| if hasattr(socket, "AF_UNIX"): | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops! I'm sorry I left that in.