• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

TCP port stuck open

Red Squirrel

No Lifer
I have a VM I use for development of a server app I am working on and as it is a dev server the app will often crash while testing new code, or act in a way that causes it to end without properly closing the port. This leaves the port stuck open and I need to reboot so I can start my app again after fixing the error.

Is there a quick way to free up such port again? The app is no longer running at this point, but the port is still open. "stop making your app do that" is not really the answer. It's in the middle of development, I'm working on it, accidents happen. 😛
 
The app itself is C++, though when the port is stuck open the app is no longer operating, so it becomes a Linux / kernel issue. There must be a command or something I can call to force that port to shut down. Restarting the network does not work. Guessing it's at the kernel level.
 
Are you sure there's nothing of the process left? Because the kernel closed all file handles (including TCP ports) when an app exits whether it's a graceful exit or not.
 
This is the state:

Code:
tcp        0      0 0.0.0.0:10000               0.0.0.0:*                   LISTEN

And yeah there is no process left of the app. Think it's because it crashes before it closes the listening socket.
 
Hmm odd, it showed that httpd was using the port but it's not, but I restarted httpd and it released it. That's really weird. Ironicly my app does manage httpd.
 
Do you also have it restart the daemon at any point? Is it possible the daemon was a child of the control panel process and thus held onto the file descriptors?
 
Hmm yeah it restarts it every 15 minutes so that could be. Though think I can probably just do a reload, I originally made it do a restart.
 
Hmm yeah it restarts it every 15 minutes so that could be. Though think I can probably just do a reload, I originally made it do a restart.

If, and this is pretty much a guess since most http daemons should be smart enough to reparent to init, it restarts it and the new process is a child of your daemon then it may inherit a copy (well, COW from the process descriptor) of the file handles of your daemon and inadvertently keep the TCP socket open since it still has a handle to it.
 
If, and this is pretty much a guess since most http daemons should be smart enough to reparent to init, it restarts it and the new process is a child of your daemon then it may inherit a copy (well, COW from the process descriptor) of the file handles of your daemon and inadvertently keep the TCP socket open since it still has a handle to it.

Hmm even if I'm calling system("service httpd restart")? I figured system forked off a totally separate process.

Not a huge biggie at this point though as when this happens it's not during normal operation of the app. If restarting Apache does the trick then that will do. If it becomes an issue in the future I can even setup the restarting as a cron job and just have my app manage the cron file. Eventually this will be a full blown server management system for web hosting. (kinda like cpanel but more basic)
 
Hmm even if I'm calling system("service httpd restart")? I figured system forked off a totally separate process.

I believe it does, but by default the fork() system call gives the resulting child copies of the parent's open file descriptors. Just look at the fork man page, it describes exactly what it does.

If it becomes an issue in the future I can even setup the restarting as a cron job and just have my app manage the cron file. Eventually this will be a full blown server management system for web hosting. (kinda like cpanel but more basic)

If you do that and it stays in there you'll likely be the only one using your control panel. I'd be pissed if something like that was restarting my webserver periodically just to paper over it's own bugs.
 
I believe it does, but by default the fork() system call gives the resulting child copies of the parent's open file descriptors. Just look at the fork man page, it describes exactly what it does.



If you do that and it stays in there you'll likely be the only one using your control panel. I'd be pissed if something like that was restarting my webserver periodically just to paper over it's own bugs.

The web server needs to be restarted regardless for changes to take effect, thought I might be able to get away with "reload". I did not try it yet. Cpanel is worse, it does it each time a user makes a change! What I'll probably end up doing in the future is make it only restart if changes were made in the last 15 minutes. Same with bind, postfix, dovecot etc...
 
Back
Top