Commit Graph

53 Commits

Author SHA1 Message Date
Fabian @ Blax Software 35ed320202 fix(fork): children never outlive the server or hold its port (#2465)
A fork-per-message child inherits the server's listening socket. A child
that outlived the server (a long-running handler such as an engine loop)
kept the port bound, so the restarted server failed with EADDRINUSE until
that child died.

- Track every forked child (Server\ChildProcesses). On hard and soft
  shutdown, send SIGTERM to the ones still running, SIGKILL them after
  websockets.shutdown.child_grace_seconds (default 2) and reap them. A PID is
  signalled only while waitpid() confirms it is still an unreaped child, with
  SIGCHLD blocked, so a recycled PID is never hit. Opt out with
  websockets.shutdown.terminate_children = false.
- Right after fork the child closes its copy of the listening socket, so even
  a stray child cannot hold the port. Only the fd is closed: the React server
  is not closed, because removing it from an ext-ev/ext-event loop would edit
  the epoll set the child shares with the parent.
- The child restores default SIGTERM/SIGINT/SIGCHLD handling. Under ext-ev
  the inherited libev handlers swallowed SIGTERM in the child.
- A child that dies of a fatal error (out of memory, E_ERROR) now sends
  <event>:error from a shutdown function (Server\ChildFatalErrorGuard,
  #1462). A child that dies without any reply (killed by a signal, or the
  shutdown function never ran) gets <event>:error from the parent, decided
  once both its stream and its exit status are in
  (Websocket\ChildMessageOutcome).
- Tests: real forks, signals and sockets for the shutdown, the port release
  and the fatal reply; end to end through Handler::forkWithSocketPair(). The
  test TestCase now stops a leftover broadcast socket, which made
  ws_available() true for later tests.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-14 10:38:11 +02:00
Fabian @ Blax Software c169e418ca fix(shutdown): soft shutdown always stops the loop
The soft shutdown chained ->then() on Handler::onClose(), which returns
void. The error became a silently dropped promise rejection, loop->stop()
never ran, and the server kept accepting and closing every new socket
without connection_established until a hard restart. Clients queued their
sends forever in that window.

- Close connections directly (Ratchet runs onClose once per stream), stop
  the loop after a 1s grace, on a listing error, or at a 5s deadline.
- Websocket\ChannelManager::unsubscribeFromAllChannels returns early for a
  connection declined before it got an app, like LocalChannelManager. That
  was the "Attempt to read property id on null" flood in the log.
- Tests: SoftShutdownTest drives the shutdown on a fresh loop, StartServerTest
  fails on any error logged during a soft shutdown, ClosedBeforeOpenTest.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 14:20:46 +02:00
Fabian @ Blax Software 1bd747835f fix(server): retry port bind on EADDRINUSE during restart handover
A restarting daemon (deploy, supervisor, steer restart-hard) often finds
the predecessor's socket still bound for a few seconds after SIGTERM.
Crashing immediately turned every restart into an EADDRINUSE crash-loop
(146 GlitchTip events on learn-atc prod). Retry the bind for up to 30s
before giving up; a port still taken after that is a real conflict and
rethrows.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-17 08:41:50 +02:00
Fabian @ Blax Software fa32057035 fix(server): reliably reap fork-per-message child processes (zombie leak)
The fork-per-message model spawns a child per inbound message; each child
exit(0)s when done. The only reaper was a single, non-looping
pcntl_waitpid(-1, WNOHANG) inside the IPC onClose callback — which fires on
socket EOF, an event that routinely precedes the child's actual process exit,
so it frequently reaped nothing and left <defunct> zombies parented to the
live serve process (where no PID 1 init can reach them). Under sustained
traffic these accumulated until pcntl_fork() itself failed.

- Handler::onClose now drains ALL exited children in a loop (was a single
  wait). activeChildCount bookkeeping stays per-socket, untouched.
- StartServer adds a SIGCHLD handler + a 10s periodic backstop (catches
  children that crashed before IPC setup, so no onClose fires) + a
  drain-on-shutdown so a deploy restart does not hand orphans to PID 1.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-02 11:06:54 +02:00
Fabian @ Blax Software dd6be893a6 BF cache-key slug drift on ws_socket_auth_*; resilient User column in watch -v
Handler::cacheAuthenticatedUser() and ::cleanupChannelConnections() were
writing/forgetting ws_socket_auth_<rawSocketId> while
WebsocketService::getAuth() and ::setUserAuthed() have always slugged
("123.456" → "123-456"). Result: the cache write was reachable from the
package's own writer path but not from the service-layer reader, so the
admin tooling (websockets:watch -v) saw cache misses and rendered #<id>
instead of the configured IdentityFormatter output.

Also: WatchStats now batch-loads missing users via the configured auth
provider model in one query per render, so the User column still renders
the full formatter shape even when the per-socket cache blob predates
the writer or got evicted.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-27 13:56:07 +02:00
Fabian @ Blax Software a46243a706 F IdentityFormatter contract — apps override how the User column renders
websockets:watch -v now delegates User-column rendering to a bound
IdentityFormatter. The package ships DefaultIdentityFormatter which
produces #<id> - <name> | <username> - <email> for typical Eloquent users
(any field absent = that segment dropped). Apps with non-User auth
subjects (Company, ApiClient, multi-tenant blobs) can implement the
contract and either bind it in their service provider or name it in
config/websockets.php as 'identity_formatter'. Resolution order is:
explicit container binding > config class > package default.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-27 13:50:22 +02:00
Fabian @ Blax Software e45d8dff20 F websockets:watch -v — per-connection rows (socket id, user, duration)
Verbose mode reuses Symfony's built-in -v flag (the short option is
reserved, so a custom one can't be added). Each channel summary is
followed by sub-rows showing the socket id, the authed user (falling
back to "Guest" or the user id if the user blob expired), and how long
the connection has been open.

Connection start times are written by Handler::establishConnection() into
ws_connection_<slug(socketId)> on open and forgotten on close. Also
fixed a latent bug in finalizeConnectionClose() where the forget key was
missing the slug() call that the reader has always used — previously
invisible because nothing wrote the key, now load-bearing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-27 13:42:26 +02:00
Fabian @ Blax Software e1abef4194 F websockets:watch — live-updating stats display, refreshes every 1s
Renders the same Live Stats / channels table that `websockets:info` shows
at the bottom, but loops indefinitely so it can be left open as a quick
status pane. 1-second poll against the existing WebsocketService cache
reads — no pub/sub plumbing because there is no "stats changed" event
emitted today and a 1s tick is fast enough for the granularity humans
care about.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-27 13:31:25 +02:00
Fabian @ Blax Software 18c58b9be6 F websocket:restart-hard — direct-signal restart that confirms PID swap
The legacy `websockets:restart` and `websocket:steer restart` rely on the
running server polling a cache key every ~5s, then unwinding the loop.
That fails silently when the cache driver differs across processes, the
poll loop stalls, or the deploy script needs to confirm the restart
happened. This adds a command that pgreps the running process, sends
SIGTERM directly (the existing PCNTL handler in StartServer already
catches it), then waits for supervisord's autorestart to bring up a new
PID before returning. Designed to be invoked from deploy scripts.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-27 13:19:38 +02:00
Fabian @ Blax Software a3194247c4 BF nullable deprecations 2026-04-23 11:56:50 +02:00
Fabian @ Blax Software 781e329601 feat: add websockets:info command with live stats 2026-04-15 10:07:59 +02:00
Fabian @ Blax Software 093bbe3a44 A tests, documentation for helpers and lifecycle
- Introduced `helpers-and-testing.md` to document global helpers and WebsocketService class usage.
- Created `HandlerLifecycleTest.php` to test the full WebSocket handler lifecycle, including connection management, channel subscriptions, and message routing.
- Added `WebsocketServiceTest.php` to validate state tracking methods in WebsocketService, covering user authentication, channel tracking, and broadcast functionality.
2026-04-02 12:44:16 +02:00
Fabian @ Blax Software 35b2731349 I max concurrency handling, BF restart 2026-03-20 13:44:51 +01:00
Fabian @ Blax Software a2a5524637 A steer 2026-03-14 09:32:24 +01:00
Fabian @ Blax Software 2a47591907 R cache, connection session, performance 2026-02-09 13:50:42 +01:00
Fabian @ Blax Software 1eafa237a0 R removed polling, transitioned to kernel events 2026-02-09 13:04:22 +01:00
Fabian @ Blax Software 826c198a77 IA helpers 2026-02-03 15:03:50 +01:00
Fabian @ Blax Software 61d4eb282e BH hrm 2026-01-26 11:52:53 +01:00
Fabian @ Blax Software 2849f0fe5f RI websocket performance 2026-01-24 14:42:35 +01:00
Fabian @ Blax Software 5b8a2a8112 I performance, A testing & event-driven 2026-01-24 13:34:29 +01:00
Fabian @ Blax Software 71e0d44e30 A logging 2026-01-20 18:17:02 +01:00
Fabian @ Blax Software 79afcdeb93 I restart logic 2026-01-20 18:11:22 +01:00
Fabian @ Blax Software 38e841c13a BF restart command 2025-12-11 11:35:59 +01:00
Fabian @ Blax Software 142f9e8bf3 BF restart command 2025-12-11 11:34:54 +01:00
Fabian @ Blax Software da82aa9fc5 IA logging, BH restart 2025-12-11 11:31:14 +01:00
Fabian @ Blax Software e470a98f69 IA logging 2025-12-11 11:25:47 +01:00
Fabian @ Blax Software 3689a084d1 IA logging 2025-12-11 11:24:59 +01:00
Fabian @ Blax Software 778d038b5a IA logging 2025-12-11 11:23:23 +01:00
a6a2f5842 7deba919b1 A sentry capture 2025-12-02 18:51:20 +01:00
a6a2f5842 c53c9d3bbd I disabled pong tracker 2025-09-15 17:32:54 +02:00
a6a2f5842 4b7c09534a I auth tracking 2025-09-15 10:23:07 +02:00
a6a2f5842 6f115a2a72 A more default settings 2025-05-08 10:54:11 +02:00
Alexander Blasl 1a5db82c26 Renovation 2025-01-16 08:54:02 +01:00
Marcel Pociot 2e4b2f35f9
Apply fixes from StyleCI (#1190) 2024-02-07 18:30:54 +01:00
Marcel Pociot 1f25913ce0
Apply fixes from StyleCI (#1044) 2022-10-06 20:42:05 +02:00
Marcel Pociot f2d3baebbd Improve logging/verbose CLI output 2022-10-06 20:41:35 +02:00
Marcel Pociot fb958fb851
Merge 2.x changes (#1043)
* Resolve conflicts

* Apply fixes from StyleCI (#1042)
2022-10-06 13:46:54 +02:00
Alex Renoki c43a9d4d46 Fixed Routes registration 2021-04-06 17:21:15 +03:00
Dries Vints ba3a2ad164
[2.x] Laravel Octane support (#733)
* Octane support

Co-authored-by: Alex Renoki <rennokki@gmail.com>
2021-04-06 16:47:54 +03:00
Alex Renoki 483ce85ef9 Don't trigger soft shutdown if no pcntl is existent. 2021-01-08 10:27:11 +02:00
Alex Renoki cbe4378086 Revert "formatting"
This reverts commit 8308a7d16d.
2020-12-07 23:30:36 +02:00
Alex Renoki 8308a7d16d formatting 2020-12-07 20:36:39 +02:00
rennokki 980f9271f0
Apply fixes from StyleCI (#526) 2020-09-15 11:30:43 +02:00
Alex Renoki 55f1332493 Added tracking for pongs 2020-09-15 12:30:17 +03:00
Alex Renoki c26e86ec2c Trigger soft-shutdown on timer restart 2020-09-13 09:37:31 +03:00
rennokki 87f5e0c316
Apply fixes from StyleCI (#522) 2020-09-12 16:45:29 +02:00
Alex Renoki ec47925c71 Added soft closes for connections on SIGTERM/SIGINT 2020-09-12 17:45:07 +03:00
Alex Renoki f04cce73d3 Added websockets:flush command 2020-09-11 16:39:56 +03:00
Alex Renoki 18dab98d87 Fixed dashboard statistics. 2020-09-11 09:07:57 +03:00
Alex Renoki 6a23016f98 Registering routes 2020-09-11 08:29:48 +03:00