Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error being started in daemon mode: Unknown: send of 5 bytes failed with errno=32 Broken pipe in file Unknown on line 0 #454

Closed
artemu78 opened this issue Aug 28, 2019 · 5 comments

Comments

@artemu78
Copy link

If i start in daemon mode i see it in console

xxxx@xxxx:/var/www/html/xxxx$ sudo php websocket.php start -d
Workerman[websocket.php] start in DAEMON mode
Unknown: send of 5 bytes failed with errno=32 Broken pipe in file Unknown on line 0
------------------------------------------- WORKERMAN --------------------------------------------
Workerman version:3.5.20          PHP version:7.0.33-0ubuntu0.16.04.3
-------------------------------------------- WORKERS ---------------------------------------------
proto   user            worker          listen                      processes    status
ssl     root            none            websocket://0.0.0.0:8000    4             [OK]
--------------------------------------------------------------------------------------------------
Input "php websocket.php stop" to stop. Start success.

Also this error in web client
Error in connection establishment: net::ERR_CONNECTION_RESET

But everything works fine if i start in DEBUG mode.
Whats the difference? Why same code works in DEBUG and does`t work in DAEMON mode?

@artemu78
Copy link
Author

I found there is something in my code.
Even daemon mode works fine if i comment all my code.
But i cant figure out why it does not work in daemon mode and work in debug mode.

I dont see any errors in console.

@walkor
Copy link
Owner

walkor commented Aug 30, 2019

I found there is something in my code.

Can you show your codes?

@artemu78
Copy link
Author

Here is code. It works in DEBUG mode and it does not works in DAEMON mode.

use Medoo\Medoo;
$database = new Medoo($db_conf);
$datas = $database->select("bots", "*", ["widget"=>$connection->token]);
$Errors = $database->error();
$connection->send(json_encode(["m" => "errors1=" . $Errors[1]]));
$connection->send(json_encode(["m" => "errors2=" . $Errors[2]]));	  

So i found out mysql error.

errors1=2006
errors2=MySQL server has gone away
Looks like my php code can`t reach mysql in daemon mode.

@walkor
Copy link
Owner

walkor commented Aug 31, 2019

Do not connect mysql before Worker::runAll(); runs.
All the codes runs before Worker::runAll(); belonging to master process.
When you connect database before Worker::runAll(); runs then all child processes will inherit database connection from master process but only one connection can be see on the mysql server side. That means the master process and all the child process share the same database connection. If one of them close the database connection all processes will be affected. Unfortunately, the master process will be shut down when daemon mode starting that why you can see the error MySQL server has gone away.

The solution is put the connecting database codes into the on{...} callback (All the on{...} callback runs after Worker::runAll and that means codes runs in the child process scope which has no impact on other processes ). Connect to database in onWorkerStart callback is recommended, for example.

$worker = new Worker('websocket://0.0.0.0:1234');
$worker->onWorkerStart = function () {
    global $database;
    $database = new Medoo($db_conf);
    $datas = $database->select("bots", "*", ["widget"=>$connection->token]);
    ....
};
$worker->onMessage = function($connection, $data){
    global $database;
   $database->select(...);
};
...
Worker::runAll();

@artemu78
Copy link
Author

artemu78 commented Sep 2, 2019

Thanks alot! It works!

@artemu78 artemu78 closed this as completed Sep 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants