Repository navigation
Conversation
Socket takes an optional timeout (default 0, which keeps today's unlimited
waits). With a timeout, connect, every read and every write fail with a
SocketException after that long without progress, so a connection that
dies silently (no FIN or RST) no longer blocks the binlog reader forever.
A packet that keeps arriving, however slowly, is still read whole, and a
handled signal does not restart the timeout.
Every socket failure is now reported as a SocketException without a PHP
warning, so a framework error handler cannot turn it into an
ErrorException that bypasses the caller's reconnect logic. A payload whose
last packet is empty (a length of exactly 16,777,215 bytes or a multiple)
is read instead of failing with SocketException("Success", 0).
The big-endian job runs the unit suite in a stock php:8.4-cli image without pcntl or sockets. The skip in setUp still ran tearDown, whose pcntl_alarm(0) then failed every test. Class-level RequiresPhpExtension skips before setUp and tearDown. The two stalled-write tests now send 32 MiB instead of 64 MiB, which still exceeds loopback socket buffers and fits the default 128M memory_limit.
|
Pushed 91287d7: the socket tests now skip through The five |
|
Opened #159 for the PHP 8.2 install failure (allows PHPUnit 11.5 alongside 12, so 8.2 support stays). |
Socketgets an optional timeout:new Socket(30.0), passed toMySQLReplicationFactorythrough its existing?SocketInterface $socketargument. The default is0, which keeps today's unlimited waits, so nothing changes unless you opt in. With a timeout, connect, each read and each write fail with aSocketExceptionafter that many seconds without progress. A packet that keeps arriving, however slowly, is still read whole, and a handled signal (EINTR) doesn't restart the timeout.Why: on a busy MySQL 8.4 binlog stream, a connection died once without the reader noticing and our consumer hung for about two hours. When no FIN or RST arrives,
socket_recv(..., MSG_WAITALL)blocks with no deadline, and the consumer stops with no error, so nothing reconnects. TCP keepalive helps only where the kernel settings are short, and it isn't an application deadline. The source sends an event or a heartbeat at least once perheartbeatPeriod, so a timeout of a few periods turns that silence into aSocketException, which the caller can treat like any other disconnect and reconnect from its last position. We have run this in production since October.Two smaller fixes in the same class, because the timeout depends on them:
SocketException(new codes 1001 to 1004 for read, write and connect timeouts and a refused timeout option, chosen above 1000 so they can't collide with an errno). Before, a failingsocket_recv()/socket_write()also raised a PHP warning, which a framework error handler (Laravel's, for example) turns into anErrorExceptionbefore theSocketExceptionis thrown, so the reconnect path never ran.readFromSocket(0)calledsocket_recv()with length 0, which returnsfalsewithout an error code, so it failed withSocketException("Success", 0)and the same event was replayed on every reconnect. It now returns''.Writes resume after a short count instead of treating it as success, and the error code comes from
socket_last_error($this->socket)instead of the global last error.Tests: 19 new unit tests in
tests/Unit/Socket/SocketTest.php, against local socket pairs, no MySQL needed: a silent peer fails within the timeout, a slow but live packet is read whole, a partly read packet that stops fails, clean and reset disconnects, recurring signals during a read or a write, the empty last packet, a large write to a slow reader, invalid timeouts, and a subclass that doesn't call the parent constructor keeps no timeout. Rebased ontomasterat ac88d1a (with #156). Unit suite: OK (270 tests, 603 assertions) on PHP 8.4. PHPStan: no errors. ECS: no findings in the changed files (ecs checkonmasteralready reports 10 in other files, which I left alone).🤖 Generated with Claude Code