Skip to content

log the requested endpoint when bind fails - #1272

Merged
gittiver merged 1 commit into
CrowCpp:masterfrom
kpaxico:fix-bind-error-log
Oct 12, 2026
Merged

gittiver merged 1 commit into
CrowCpp:masterfrom
kpaxico:fix-bind-error-log

Conversation

@kpaxico

@kpaxico kpaxico commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Fixes #1271.

When bind fails in the Server constructor, the error is logged with acceptor_.address() and acceptor_.port(). Both call local_endpoint() without an error code, on the acceptor that the failed bind left unbound. On Windows, the call throws (WSAEINVAL) in the middle of the log statement: the original bind error is lost, startup_failed_ is never set, and app.run() throws instead of returning with is_bound() false. On Linux, it does not throw, but the message reports 0.0.0.0:0 instead of the requested address and port. This PR logs the requested endpoint instead, as the line did before the Unix socket support (45d3fd5). Asio writes both endpoint types to a stream: a TCP endpoint as its address and port, a Unix socket endpoint as its path.

The new test server_port_in_use, next to server_invalid_ip_address, holds a loopback port with a second socket (without SO_REUSEADDR) and serves the app on that port. It captures the log with an ILogHandler and checks that run() returns without an exception, that is_bound() is false, and that the error line reads Failed to bind to 127.0.0.1:<port> - <reason>. It sets the log level it needs and restores the level and the default handler afterwards, so it does not depend on the order of the tests.

Tested

  • Windows 11, MSVC 2022 (19.44), standalone Asio 1.24.0: the new test fails before the change (local_endpoint: An invalid argument was supplied.) and passes after it. ctest -C Release passes (139 test cases).
  • Linux (Debian 12 container), GCC 12.2, standalone Asio 1.22.1 from Debian's libasio-dev: the new test fails before the change (Failed to bind to 0.0.0.0:0 - Address already in use) and passes after it. ctest passes (crow_test and template_test).

Not tested

  • CROW_USE_BOOST and macOS were not tested locally. The CI matrix covers both.
  • SSL and compression were off in my local builds. The CI builds with both on.

Thanks!

- The bind error was logged with `acceptor_.address()` and `acceptor_.port()`, which call `local_endpoint()` on the acceptor that the failed `bind` left unbound.
- On Windows that call throws, so the original bind error is lost, `startup_failed_` is never set and `app.run()` throws. On Linux the message reports `0.0.0.0:0`.
- Log the requested `endpoint` instead; Asio streams both the TCP and the Unix socket endpoint.
- Add the `server_port_in_use` test: a bind to a port that another socket holds is logged with the requested address and port, and `run()` returns with `is_bound()` false.

Fixes CrowCpp#1271
@gittiver
gittiver merged commit 8ad4d3a into CrowCpp:master Oct 12, 2026
20 checks passed
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.

Bind failure logging throws on Windows and reports 0.0.0.0:0 on Linux

2 participants