Problem: a program has a socket open and you want to force it to close without killing the owning process.
Solution: get the file descriptor of the socket, debug the process and manually call close on the file descriptor.
On Linux systems:
- Find the offending process:
netstat -np
- Find the socket file descriptor:
lsof -np $PID
- Debug the process:
gdb -p $PID
- Close the socket:
call close($FD)
- Close the debugger:
quit
- Profit.
From here.
On OSX the incantations are differently formed:
- Find the offending process and file descriptor:
lsof -ni TCP
- Debug the process:
lldb -p $PID
- Close the socket:
call (int)close($FD)
- Close the debugger:
quit
- Profit.