Problem:
I am trying to figure out if I really need to implement any thread safe mechanism when dealing with a client with multiple threads accessing the same (client) socket, and the information I find seems contradictory.
- Every implementation I find consist basically in a server spawning new threads to hanlde each client's connection. For example: How to make a simple multithreaded socket server in Python that remembers clients
And as you can see, there is no explicity thread-safety mechanism.
- However, according to this answer, sockets are not thread safe (and I have read that under some conditions there can be problems when using the same socket although nothing clear to me): Python: Socket and threads?
Context
The setup I need to deploy to production is a FastAPI server that accepts http requests, and then, as a client, makes requests to a server socket maintaining a sinlge socket regardless of http clients. My routes are not async, and that means parallel http requests from the frontend (or frontends) will be handled as different threads by FastAPI and then those threads will use the same socket to send data to the socket. The problem is that I have experienced "broken pipe" errors some times and I don't really know where the weak point of the whole setup might be. These errors occur in the client socket end of the FastAPI appplication, when handling more than one request at the same time.
In short, I would like to know if someone has suggestions in terms of thread safety for the whole setup.