@@ -166,7 +166,15 @@ public Server start(@NonNull Jooby... application) {
166166 var http =
167167 newBootstrap (
168168 allocator , transport , newPipeline (options , null , http2 , grpcProcessor ), eventLoop );
169- http .bind (options .getHost (), options .getPort ()).get ();
169+
170+ var httpFuture = http .bind (options .getHost (), options .getPort ());
171+ if (options .getPort () == 0 ) {
172+ httpFuture .get (); // Wait for bind to complete
173+ java .net .SocketAddress address = httpFuture .channel ().localAddress ();
174+ if (address instanceof java .net .InetSocketAddress ) {
175+ options .setPort (((java .net .InetSocketAddress ) address ).getPort ());
176+ }
177+ }
170178 }
171179
172180 if (options .isSSLEnabled ()) {
@@ -184,7 +192,14 @@ public Server start(@NonNull Jooby... application) {
184192 newPipeline (options , sslContext , http2 , grpcProcessor ),
185193 eventLoop );
186194 portInUse = options .getSecurePort ();
187- https .bind (options .getHost (), portInUse ).get ();
195+ var httpsFuture = https .bind (options .getHost (), portInUse );
196+ if (portInUse == 0 ) {
197+ httpsFuture .get ();
198+ var address = httpsFuture .channel ().localAddress ();
199+ if (address instanceof java .net .InetSocketAddress inetSocketAddress ) {
200+ options .setSecurePort (inetSocketAddress .getPort ());
201+ }
202+ }
188203 } else if (options .isHttpsOnly ()) {
189204 throw new StartupException ("Server configured for httpsOnly, but ssl options not set" );
190205 }
0 commit comments