Vapor: Setting server address with nil port causes crash

Created on 22 Aug 2020  Â·  3Comments  Â·  Source: vapor/vapor


Steps to reproduce

Set the server configuration address to an address with a nil port in configure.swift.

app.http.server.configuration.address = .hostname("0.0.0.0", port: nil)



Expected behavior

I was trying to be able access the server from another device by explicitly opening all ports (following this: https://forums.raywenderlich.com/t/can-you-access-the-vapor-server-from-another-device-when-running-locally/52811). I ended getting this to work by using run arguments instead.

Actual behavior

The server crashes while starting up. It looks like this crash is supposed to be avoided:

image

Environment


  • Vapor Framework version: 4
  • OS version: MacOS 11.0 Beta 5 (and also Xcode 12 Beta 5)
bug

Most helpful comment

Regarding the issue you are seeing, I believe if you bind to localhost (the default), you are limiting connections to clients on the same machine the server is running on. By binding to 0.0.0.0, you aren't necessarily opening all ports, but opening connections on all _interfaces_ (for instance, connections coming from your WiFi or wired ethernet networks) — you'll still need a port for those clients to connect to though. The fix in #2479 addresses the crash and fixes this behavior where specifying a nil port will go ahead and use the default port, which is 8080.

All 3 comments

I think what you want is to use an actual port. Let's say 8000 and pass the hostname as 0.0.0.0 as you had. Then devices on the same network can connect by hitting 0.0.0.0:8000/my-route

This is likely a regression - setting the port to nil should use the default port, and shouldn’t crash here. The fix is easy, so I’ll submit a PR with a test later today since I’m likely the one that introduced it 😅

Regarding the issue you are seeing, I believe if you bind to localhost (the default), you are limiting connections to clients on the same machine the server is running on. By binding to 0.0.0.0, you aren't necessarily opening all ports, but opening connections on all _interfaces_ (for instance, connections coming from your WiFi or wired ethernet networks) — you'll still need a port for those clients to connect to though. The fix in #2479 addresses the crash and fixes this behavior where specifying a nil port will go ahead and use the default port, which is 8080.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

betzerra picture betzerra  Â·  3Comments

mandrusiaks picture mandrusiaks  Â·  4Comments

xiaoyifan picture xiaoyifan  Â·  3Comments

Joannis picture Joannis  Â·  4Comments

nsleader picture nsleader  Â·  4Comments