Currently I'm working on a new task manager that can create/fork new processes and execute given tasks from the database.
This is part of a php(7.4.18 custom built) project with mongodb(4.2 database, 1.9.1 php extension, 1.5.2 mongodb/mongodb wrapper package and 2.1.11 yiisoft/yii2-mongodb wrapper package)
So the way it works is that I'm pooling the database in every X seconds and if I can see at least 1 task then I get it in the parent process, then if there is enough free memory for a new task, then I fork the process (with pcntl) and execute the task in the child process.
The problem is that I get an error message in the logs, saying: Failure during socket delivery: Broken pipe (32)
Now what I think is that it might be because forking copies the memory stack of the parent process, and then both the parent and all the child processes are trying to use the exact same connection to the database. And once one of the child process finishes it exits and closes the connection, which - since it's used by all the processes - will be unavailable for the other processes.
I think mine isn't a unique case, so I would like to know how to solve this issue?
I've found a few previous bug reports:
But I'm not sure what is the official best way to go around this.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…