TCP TIME_WAIT causing 'address already in use' error

preview_player
Показать описание

Build Your Own Redis / DNS / BitTorrent / SQLite - with CodeCrafters.

# Recommended videos and playlists

If you liked this video, you will find the following videos and playlists helpful

# Things you will find amusing

# Other socials

I keep writing and sharing my practical experience and learnings every day, so if you resonate then follow along. I keep it no fluff.

Thank you for watching and supporting! it means a ton.

I am on a mission to bring out the best engineering stories from around the world and make you all fall in
love with engineering. If you resonate with this then follow along, I always keep it no-fluff.
Рекомендации по теме
Комментарии
Автор

Nice one! waiting for more such shorts, sharing some deep tech things for learning

hemsagarpatel
Автор

Same thing happened to me while trying to run ollama. I kept wondering why it says address already in use even though ollama is stopped.
Now I know why that happened. Thanks.

iajaydandge
Автор

I also faced same where lsof -t -i:port will not give the output of the process id,
By using netstat command I am able to see that TIME_WAIT state

learnsuvvu
Автор

Nice one. but subtitle is behind the title and description. making it hard to read.

VAIBHAVVARSHNEY-pj
Автор

I love networking and have been trying to go much deeper in the domain. This is something which was very new to me. Do you mind sharing resources with which you were able to dive so much deeper into the topic?

ayushshah
Автор

Hi Arpit
I have a question can a Devops engineer can learn System Design?

nithinkumar
Автор

does the same happen with springboot applications as well?

manjosh
Автор

Happened for me while running my nodejs app. I have closed the terminal without killing the process. When tried to start again, it threw address already in use. Then I had to force kill the process to start the app again. Is this similar scenario?

logeshkumarrajkumar
Автор

Apparently if the client initiates the "connection close", then the client socket would go into TIME_WAIT. While the server port can be re-used immediately.

vijaynala
Автор

Great information. I first came across time_wait state of socket in "understanding distributed system" book. Here's the snippet I highlighted while going through it.


After the data transmission is complete, the connection needs to be closed to release all resources on both ends. This termination phase involves multiple round-trips. If it's likely that another transmission will occur soon, it makes sense to keep the connection open to avoid paying the cold-start tax again.

Moreover, closing a socket doesn't dispose of it immediately as it transitions to a waiting state (TIME_WAIT) that lasts several minutes and discards any segments received during the wait. The wait prevents delayed segments from a closed connection from being considered part of a new connection. But if many connections open and close quickly, the number of sockets in the waiting state will continue to increase until it reaches the maximum number of sockets that can be open, causing new connection attempts to fail. This is another reason why processes typically maintain connection pools to avoid recreating connections repeatedly.

mritunjaykumar