Skip to content

Add .sock cleanup to ensure Studio uses new _events after unclean shutdown - #2847

Merged
wojtekn merged 3 commits into
trunkfrom
add-brute-force-sock-cleanup
Mar 19, 2026
Merged

Add .sock cleanup to ensure Studio uses new _events after unclean shutdown#2847
wojtekn merged 3 commits into
trunkfrom
add-brute-force-sock-cleanup

Conversation

@wojtekn

@wojtekn wojtekn commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Related issues

  • N/A

How AI was used in this PR

I used it for debugging the issue and to add code that removes socket file.

Proposed Changes

I propose adding .sock cleanup to ensure Studio uses new _events after an unclean shutdown.

In the other case, when an old socket is not cleaned up after an unclean shutdown, Studio will reuse it.

Testing Instructions

  • Start Studio, start sites, confirm they are shown as running

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?
@wojtekn
wojtekn requested a review from fredrikekelund March 19, 2026 13:01
@wojtekn wojtekn self-assigned this Mar 19, 2026
@wojtekn
wojtekn marked this pull request as ready for review March 19, 2026 13:01
Comment on lines +111 to +118
// Remove any stale socket from a previous Studio session. Studio is single-instance,
// so any existing events.sock belongs to a dead session and must be replaced.
try {
fs.unlinkSync( SITE_EVENTS_SOCKET_PATH );
} catch ( err ) {
// ENOENT is fine — socket didn't exist. Any other error is unexpected but non-fatal.
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do this only on non-Windows platforms. On Windows, SITE_EVENTS_SOCKET_PATH isn't actually a file system path

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Thinking about it more, we could move this to eventsSocketServer.listen(), making the cleanup the SocketServer's responsibility rather than the caller's. Do you see anything against that?

@fredrikekelund fredrikekelund Mar 19, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SocketServer::listen already has some clever error-handling logic inspired by pm2-axon. If listening on the socket fails, it'll try connecting as a client to see whether the socket has a server on the other end (if not, then the only connected parties are clients). If it cannot connect, then it'll remove the socket file and try again.

This is why I was slightly surprised that we need this additional layer of defense, but clearly, there are scenarios when there are stray processes that we need to handle.

So, in short, we shouldn't assume that the socket file can always be deleted in SocketServer::listen.

However, I do think SocketServer::close could use additional logic to remove the socket file as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic won't remove the old socket if it can connect to it, and the issue may arise when an outdated server is on the other end.

It makes sense to keep SocketServer::listen generic and keep the brute-force cleanup logic as part of the _event command, since this command never needs to attach to an existing socket.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a guard for the Windows.

@wpmobilebot

wpmobilebot commented Mar 19, 2026

Copy link
Copy Markdown
Collaborator

📊 Performance Test Results

Comparing e8cf0df vs trunk

app-size

Metric trunk e8cf0df Diff Change
App Size (Mac) 1236.41 MB 1236.44 MB +0.03 MB ⚪ 0.0%

site-editor

Metric trunk e8cf0df Diff Change
load 1848 ms 1911 ms +63 ms 🔴 3.4%

site-startup

Metric trunk e8cf0df Diff Change
siteCreation 7069 ms 6083 ms 986 ms 🟢 -13.9%
siteStartup 3927 ms 3917 ms 10 ms ⚪ 0.0%

Results are median values from multiple test runs.

Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change (<50ms diff)

@fredrikekelund fredrikekelund left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could use this PR as an opportunity to also remove the socket file in SocketServer::close, but we could also do that separately. This change by itself should fix the issue at hand

@wojtekn
wojtekn merged commit 9ab26aa into trunk Mar 19, 2026
13 checks passed
@wojtekn
wojtekn deleted the add-brute-force-sock-cleanup branch March 19, 2026 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants