There is a program on python. I listen to a socket (I run from under the root). When you first start everything runs smoothly. At runtime, the program crashes. And I can't listen to the socket a second time. The system says the port is busy. Ie, probably, a socket is hanging somewhere in the background.

Probably, I somehow incorrectly close the socket. close() is not a solution. Probably, it somehow needs to be deleted, or something else to do with it.

Code:

 #! /usr/bin/env python # -*- coding: utf-8 -*- import socket print("python: start") # Сервер для браузера print('python: Test 0') sock = socket.socket() sock.bind(("127.0.0.1", 8080)) # Падает здесь 

Mistake:

 python: start python: Test 0 Traceback (most recent call last): File "/mapsfullsearch/src/estimation/main.py", line 12, in <module> sock.bind(("127.0.0.1", 8080)) File "/usr/lib/python2.7/socket.py", line 228, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 98] Address already in use ^CMakefile:130: recipe for target 'estimation' failed make: *** [estimation] Interrupt 
  • It will be released after some time (I do not remember exactly the semantics, but it seems that the OS leaves a grace period to clean up everything). If memory serves, SO_REUSEPORT will capture the port immediately. I remember all this very vaguely, and studied at the dawn of youth, so in general there may be some errors in the description above. - etki
  • You can give the command netstat -na | grep :8080 netstat -na | grep :8080 after completion of the program. It is quite possible that you will see suspended connections in the state, most likely FIN_WAIT. Until all such "connections" disappear, opening the port again will not work. close () closes the listening socket, but if connections were opened (they have their own sockets), then the parties must exchange closing packets (FIN), and if all connections were not correctly closed and they were not applied (the actual closure occurs after some time) and FIN will not be exchanged, timeouts will be expected - Mike
  • To speed up the closure of connections, at least in C (as I don’t know in python), close all sockets (workers listening to nothing here) are recommended to shutdown(socket, SHUT_RDWR) to reset all buffers and send FIN - Mike
  • @Mike, you had to add socket.close() . But this does not solve the problem of an emergency interruption by the user KeyboardInterrupt . netstat -na | grep :8080 netstat -na | grep :8080 does not help. If you look at it through nc , then ok. The port is used. But not netstat . - hedgehogues

1 answer 1

Kill all processes on HOST (ubuntu):

 sudo lsof -t -i tcp:HOST | xargs kill -9 

PS That error that you had in the post arose from the fact that the python cannot open the port as it is already occupied by someone. "socket.error: [Errno 98] Address already in use"

  • I understand that he is busy with someone. But netstat it (someone does not see this). You can see the availability of the process at 8080 through nc, but this does not solve the problem. I am afraid that this question has ceased to be relevant to me. I avoided the problem differently: put the flask and use it. - hedgehogues
  • Are you running netstat as root? - Andrio Skur