@@ -66,7 +66,27 @@ public function connect($uri)
6666 return \React \Promise \reject (new \InvalidArgumentException ('Invalid target URI ' ));
6767 }
6868
69- $ process = new Process ($ this ->cmd . ' -W ' . \escapeshellarg ($ parts ['host ' ] . ': ' . $ parts ['port ' ]));
69+ $ command = $ this ->cmd . ' -W ' . \escapeshellarg ($ parts ['host ' ] . ': ' . $ parts ['port ' ]);
70+
71+ // try to get list of all open FDs (Linux only) or simply assume range 3-1024 (FD_SETSIZE)
72+ $ fds = @scandir ('/proc/self/fd ' );
73+ if ($ fds === false ) {
74+ $ fds = range (3 , 1024 ); // @codeCoverageIgnore
75+ }
76+
77+ // do not inherit open FDs by explicitly closing all of them
78+ foreach ($ fds as $ fd ) {
79+ if ($ fd > 2 ) {
80+ $ command .= ' ' . $ fd . '>&- ' ;
81+ }
82+ }
83+
84+ // default `sh` only accepts single-digit FDs, so run in bash if needed
85+ if ($ fds && max ($ fds ) > 9 ) {
86+ $ command = 'exec bash -c ' . escapeshellarg ($ command );
87+ }
88+
89+ $ process = new Process ($ command );
7090 $ process ->start ($ this ->loop );
7191
7292 $ deferred = new Deferred (function () use ($ process , $ uri ) {
@@ -110,9 +130,6 @@ public function connect($uri)
110130 }
111131
112132 $ connection = new CompositeConnection ($ process ->stdout , $ process ->stdin );
113- $ connection ->on ('close ' , function () use ($ process ) {
114- //$process->terminate();
115- });
116133 $ deferred ->resolve ($ connection );
117134 });
118135
0 commit comments