Skip to content
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

Restart on EADDRINUSE #1890

Closed
tshemsedinov opened this issue Aug 30, 2023 · 4 comments
Closed

Restart on EADDRINUSE #1890

tshemsedinov opened this issue Aug 30, 2023 · 4 comments
Assignees

Comments

@tshemsedinov
Copy link
Member

tshemsedinov commented Aug 30, 2023

Describe the problem

uncaughtException: Error: listen EADDRINUSE: address already in use 0.0.0.0:8000
  Server.setupListenHandle [as _listen2] (node:net:1463:16)

Describe the solution

Port is in use by another server, so we can:

  • wait for timeout
  • try 3 times
  • exit 1
@MarhiievHE
Copy link
Member

MarhiievHE commented Sep 24, 2023

@tshemsedinov It looks like the HTTP server is not returning this error via the error listener. Will it be ok to catch this error in this way in worker.js, or do we need to find a more elegant solution?:

    process.on('uncaughtException', function(err) {
      if (err.code === 'EADDRINUSE') {
        console.warn(`Address in use: ${options.host}:${port}, retry...`);
        setTimeout(() => {
          tryToStartCounter++;
          if (tryToStartCounter > 3) process.kill(process.pid, 'SIGINT');
          application.server = new metarhia.metacom.Server(application, options);
        },options.timeouts.bind);
      }
    });

https://github.com/MarhiievHE/impress/blob/594c068dc9e97a9c3734dcc44251e32814aa5492/lib/worker.js#L86-L102

@MarhiievHE
Copy link
Member

@tshemsedinov It looks like the HTTP server is not returning this error via the error listener. Will it be ok to catch this error in this way in worker.js, or do we need to find a more elegant solution?:

    process.on('uncaughtException', function(err) {
      if (err.code === 'EADDRINUSE') {
        console.warn(`Address in use: ${options.host}:${port}, retry...`);
        setTimeout(() => {
          tryToStartCounter++;
          if (tryToStartCounter > 3) process.kill(process.pid, 'SIGINT');
          application.server = new metarhia.metacom.Server(application, options);
        },options.timeouts.bind);
      }
    });

https://github.com/MarhiievHE/impress/blob/594c068dc9e97a9c3734dcc44251e32814aa5492/lib/worker.js#L86-L102

I was wrong, that wasn't the point. The problem was that a wsServer was created in addition to the httpServer, and the error occurred in the wsServer, so the httpServer error listener did not catch it.

@MarhiievHE
Copy link
Member

MarhiievHE commented Sep 27, 2023

It makes three tries to open the server on the specified port with bind timeout and then sends SIGTERM, but it doesn`t stop the application with error code 1!

image

tshemsedinov added a commit to metarhia/metacom that referenced this issue Oct 3, 2023
Refs: metarhia/impress#1890
PR-URL: #455
Co-authored-by: Timur Shemsedinov <[email protected]>
@tshemsedinov tshemsedinov reopened this Oct 3, 2023
@tshemsedinov
Copy link
Member Author

Fixed in #1908 and published 3.0.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants