question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

reuse address on server

See original GitHub issue

start_hpp_server should reuse the port if it is already being used, most likely due to an earlier instance going down and we are restarting

File "lib64/python3.4/site-packages/prometheus_client/exposition.py", line 103, in run
    httpd = HTTPServer((addr, port), MetricsHandler)
  File "/usr/lib64/python3.4/socketserver.py", line 430, in __init__
    self.server_bind()
  File "/usr/lib64/python3.4/http/server.py", line 136, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/lib64/python3.4/socketserver.py", line 444, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
itldocommented, May 27, 2020

I’ve been living with my supervisord workaround described in my last post. I had made my proposed change in my own build, which appeared also to work, however I never took the time to submit it, as my workaround was meeting my needs. I need to consider doing that. -tim

0reactions
tommyjcarpentercommented, Nov 16, 2022

A simple fix for this issue might be to have httpd as a global that can be accessed - we are doing something similar. That way the caller can call httpd.shutdown() in a signal handler. Something like:

httpd: Optional[HTTPServer] = None

def handler(signum: int, _frame: Any) -> None:
    """gracefully shutdown servers on signal"""
    signame = signal.Signals(signum).name
    logger.warn(f"Signal handler called with signal {signame} ({signum})")
    if httpd:
        httpd.shutdown()
        logger.info("Shutdown metrics server")

def start_http_server():
    global httpd
    httpd = HTTPServer(("", 8080), SomeHandler)
    server = threading.Thread(name="metrics", target=httpd.serve_forever)
    server.daemon = True
    server.start()
    logger.debug("Servers started")

client program:

# Set the signal handler
def __main__():
    ...
    signal.signal(signal.SIGINT, handler)  # ctrl-c
    signal.signal(signal.SIGTERM, handler)  # what kubernetes sends for graceful shutdown
Read more comments on GitHub >

github_iconTop Results From Across the Web

Reusing socket addresses | Python Network Programming ...
Reusing socket addresses. You want to run a socket server always on a specific port even after it is closed intentionally or unexpectedly....
Read more >
Reusing address in socket - how does it actually work
My understanding is that "socket" can be option to let "bind " to reuse port or address. It does not. But "bind" will...
Read more >
Java server socket doesn't reuse address - Stack Overflow
I set the reuse address option of the server socket before the binding but it still throws a BindException.
Read more >
What exactly does SO_REUSEADDR do? - UnixGuide.net
It is useful if your server has been shut down, and then restarted right away while sockets ... SO_REUSEADDR just says that you...
Read more >
Using SO_REUSEADDR and SO_EXCLUSIVEADDRUSE
An additional feature called enhanced socket security is available on Windows Server 2003 and later. The availability of these socket options ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found