Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
284 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should be creating a new client instance from the process where you are using it.

See here for how this generally is supposed to work, and here if you are in an environment where you cannot easily create a new client instance.

Forking in php isn't very common as far as I know, so you should figure out whether you can create new clean slate clients in your child processes and do that if possible, otherwise handle the error and reconnect.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...