Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

php - Forked process: Failure during socket delivery: Broken pipe

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...