Commit 3cb0ddd
committed
Fix possible
Multiple socket servers can listen on the same socket address (using
`SO_REUSEPORT` or shared file descriptors). Accordingly, when a new
connection is incoming, multiple event-loops could report the socket to
be readable and try to a `accept()` an incoming connection from the same
socket.
This wouldn't be an issue with the underlying `accept()` system call.
The first call would succeed and subsequent calls would report `EAGAIN`
or `EWOULDBLOCK` for the other servers given the socket resource is in
non-blocking mode.
However, PHP's `stream_socket_accept()` implementation first runs a
`poll()` on the socket resource before performing an `accept()`. This
means multiple instances listening on the same address could end up in a
race condition where some may be "stuck" in the pending `poll()`.
We work around this by specifiying a `0` timeout to ensure the `poll()`
doesn't block in this case. This allows all servers to either complete
successfully or report an error and continue processing right away.accept() race condition1 parent acdcab8 commit 3cb0ddd
File tree
4 files changed
+12
-4
lines changed- src
- tests
4 files changed
+12
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
204 | 204 | | |
205 | 205 | | |
206 | 206 | | |
207 | | - | |
| 207 | + | |
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
109 | | - | |
| 109 | + | |
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
280 | 280 | | |
281 | 281 | | |
282 | 282 | | |
283 | | - | |
| 283 | + | |
| 284 | + | |
284 | 285 | | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
285 | 289 | | |
286 | 290 | | |
287 | 291 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
285 | 285 | | |
286 | 286 | | |
287 | 287 | | |
288 | | - | |
| 288 | + | |
| 289 | + | |
289 | 290 | | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
290 | 294 | | |
291 | 295 | | |
292 | 296 | | |
| |||
0 commit comments