D
Deno

help

Am I closing this server wrongly? The port is not free for a new listener! (`AddrInUse`)

GCSGuilherme C. Souza10/22/2022
const server = Deno.listen({ port: 80 });

(async function(){

for await(const conn of server){

(async function(){

const httpConn = Deno.serveHttp(conn);

for await(const reqEvent of httpConn){

reqEvent.respondWith(new Response('res.stream', {
status: 200
}));

}

})();

}

})();

const res = await fetch('http://localhost');
res.body?.cancel()
server.close();

// Port 80 is still in use here??

const newServer = Deno.listen({ port: 80 });
newServer.close();
const server = Deno.listen({ port: 80 });

(async function(){

for await(const conn of server){

(async function(){

const httpConn = Deno.serveHttp(conn);

for await(const reqEvent of httpConn){

reqEvent.respondWith(new Response('res.stream', {
status: 200
}));

}

})();

}

})();

const res = await fetch('http://localhost');
res.body?.cancel()
server.close();

// Port 80 is still in use here??

const newServer = Deno.listen({ port: 80 });
newServer.close();
DDoctor10/22/2022
Looking at the docs I'd guess so, but it probably needs to sleep for a second for stuff outside of javascript to release it. Try doing await new Promise(a => setTimeout(_ => a(true), 0)) just after the server.close()
GCSGuilherme C. Souza10/22/2022
It does help. Shouldn't Deno take care of it internally? Hopefully with some Promise I can wait on to make sure the port is released? This does work as expected in Node. Meaning I can bring a new server up once the close event is fired in the old one.
DDoctor10/22/2022
Idk. I’m guessing Deno releases it on its end and the OS just needs a bit of time to validate it or whatever. If sleeping for 0ms worked then you could wrap that Deno close in a promise.
await new Promise(a => {
server.close()
setTimeout(_ => a(true), 0)
})
await new Promise(a => {
server.close()
setTimeout(_ => a(true), 0)
})

Looking for more? Join the community!

Recommended Posts
Sync uuid from stringHow can I generate a UUID from a string synchronously? In the standard library I only found this asVS Code, Hello World -- F5 shows nothing in console?I have a simple hello world program: ```ts let message: string = 'Hello World'; console.log(messageHow to transform a Readable Stream to a file.I have a ReadableStream from a post request and i cant find a way to transform the body(ReadableStreSilence error logs on Oak serverI'm trying to silence specific errors on an Oak server on Deploy. A Github issue said to add an 'errAre pointers supported in Fast API return types?If no, how do I return pointers?Reddit fetch Vs. My noob brain part. IHello! So I want to fetch Reddit posts. I use this library. I have a few questions. https://github.cHow do I detect mouse clicks and key presses with Deno.stdin?I don't even really have any base code, so I was just wondering as to how do I do the problem specifML with FFISuppose you wish to do some machine learning stuff with Deno, would it be optimal to use FFI with C Is there a way to determine what modules have been dynamically imported at runtime?The builtin tools to compile/bundle/vendor don't account for dynamic imports (understandably), but iweird interfacehow can a class implement this interface ```ts interface I { (): void type: string }``` or a `cnpm specifier import typesso when I do something like ```ts import * as rx from 'npm:rxjs' console.log(rx) ``` I do have the error Unsupported scheme node for module `nodemodule`. Supported schemesI'm trying to import a module using: ```js import { example } from "npm:example"; ``` Inside `examplesbuild_deno_loader importing Deno modulesA dependancy I use imports modules like "fs" and I want esbuild_deno_loader to treat them as externaStatic compilation of deno modulesIs there a way to statically compile libraries into the .js bundle instead of dynamically retrieving