@@ -66,7 +66,27 @@ public function connect($uri)
66
66
return \React \Promise \reject (new \InvalidArgumentException ('Invalid target URI ' ));
67
67
}
68
68
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 );
70
90
$ process ->start ($ this ->loop );
71
91
72
92
$ deferred = new Deferred (function () use ($ process , $ uri ) {
@@ -110,9 +130,6 @@ public function connect($uri)
110
130
}
111
131
112
132
$ connection = new CompositeConnection ($ process ->stdout , $ process ->stdin );
113
- $ connection ->on ('close ' , function () use ($ process ) {
114
- //$process->terminate();
115
- });
116
133
$ deferred ->resolve ($ connection );
117
134
});
118
135
0 commit comments