Skip to content

fix(next-plugin): keep each coordinator's port and recover from restarts - #917

Merged
owjs3901 merged 1 commit into
mainfrom
fix/next-coordinator-client
Nov 17, 2026
Merged

owjs3901 merged 1 commit into
mainfrom
fix/next-coordinator-client

Conversation

@owjs3901

@owjs3901 owjs3901 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes NXT-04, NXT-05, NXT-06 from #686 and NXT-16 (read side) from #694. Merged the coordinator HTTP clients that the Turbopack source loader and the CSS loader each had into a single coordinator-client.ts.

  • NXT-04: the CSS loader cached only one port, so a second app in the same process received the first app's CSS. Ports are now cached per port file path.
  • NXT-05: when the coordinator restarted, the loader failed the build immediately (recovering only from the next file). Now, when a connection is refused, it drops the cached port and re-reads the port file. If there is a new port, it resends the request once. If it's the same port, or the new port also refuses, it's the "Coordinator unreachable" error as before.
  • NXT-06: requests had no timeout, so a coordinator that accepted a connection and never responded stalled the build forever. Now it ends with an error if there is no response for 300 seconds. That is much longer than the time the coordinator waits on purpose (/css wait, default maxWaitMs 60 seconds).
  • NXT-16 (read side): reading a port file while it was being written cached NaN as the port. Now, if it isn't an integer from 1 to 65535, it isn't cached and the existing wait (50ms × 20) continues. If there is still no valid port at the end, the error points at that file.

Remaining

New errors

  • Coordinator port file <file> names no valid port. Restart the dev server or build so the coordinator writes it again.
  • Coordinator at 127.0.0.1:<port> (port file <file>) sent no response for 300s. It is stuck or another process holds the port: restart the dev server or build.

Test plan

  • New coordinator-client.test.ts (real HTTP server): per-port-file cache, POST body, following a coordinator restarted on a new port, same port refused again, new port also refused, port file gone after restart, waiting on a port file being written, out-of-range port, read failure, timeout on an unresponsive coordinator. 100% coverage of coordinator-client.ts
  • Existing loader.test.ts, css-loader.test.ts, coordinator-port.test.ts pass
  • Full CI and coverage

요약

#686의 NXT-04, NXT-05, NXT-06과 #694의 NXT-16(읽는 쪽)을 고칩니다. Turbopack 소스 로더와 CSS 로더가 각자 갖던 coordinator HTTP 클라이언트를 coordinator-client.ts 하나로 합쳤습니다.

  • NXT-04: CSS 로더가 port를 하나만 캐시해서, 같은 프로세스의 두 번째 앱이 첫 번째 앱의 CSS를 받았습니다. 이제 port file 경로마다 따로 캐시합니다.
  • NXT-05: coordinator가 재시작하면 로더가 바로 빌드를 실패시켰습니다(다음 파일부터만 복구). 이제 연결이 거부되면 캐시한 port를 버리고 port file을 다시 읽습니다. 새 port가 있으면 요청을 한 번 다시 보냅니다. 같은 port거나 새 port도 거부하면 기존처럼 "Coordinator unreachable" 오류입니다.
  • NXT-06: 요청에 timeout이 없어, 연결을 받고 응답하지 않는 coordinator가 빌드를 영원히 멈췄습니다. 이제 응답이 300초 동안 없으면 오류로 끝냅니다. coordinator가 일부러 기다리는 시간(/css 대기, 기본 maxWaitMs 60초)보다 훨씬 깁니다.
  • NXT-16(읽는 쪽): 쓰는 중인 port file을 읽으면 NaN을 port로 캐시했습니다. 이제 1~65535 정수가 아니면 캐시하지 않고 기존 대기(50ms × 20)를 계속합니다. 끝까지 유효한 port가 없으면 그 파일을 가리키는 오류입니다.

남은 것

  • NXT-16의 쓰는 쪽(임시 파일 + rename, /health 토큰 확인)은 하지 않았습니다. Windows에서 다른 프로세스가 읽는 중인 파일로 rename하면 EPERM이 날 수 있습니다. 읽는 쪽 검증으로 잘린 파일은 막힙니다.
  • NXT-07(프로세스마다 coordinator 하나)은 그대로입니다. 엔진 상태(sheet, class map)도 프로세스 전역이라, 한 프로세스의 두 앱을 제대로 분리하려면 엔진 세션이 먼저 필요합니다.
  • #755와 겹치지 않습니다(#755는 이 클라이언트 코드를 바꾸지 않습니다). 닫힌 #752가 coordinator 세션을 다뤘지만 병합되지 않았습니다.

새로 생기는 오류

  • Coordinator port file <file> names no valid port. Restart the dev server or build so the coordinator writes it again.
  • Coordinator at 127.0.0.1:<port> (port file <file>) sent no response for 300s. It is stuck or another process holds the port: restart the dev server or build.

테스트 계획

  • 새 coordinator-client.test.ts(실제 HTTP 서버): port file별 캐시, POST body, 새 port로 재시작한 coordinator 따라가기, 같은 port 재거부, 새 port도 거부, 재시작 후 port file 사라짐, 쓰는 중인 port file 대기, 범위 밖 port, 읽기 실패, 응답 없는 coordinator timeout. coordinator-client.ts 커버리지 100%
  • 기존 loader.test.ts, css-loader.test.ts, coordinator-port.test.ts 통과
  • CI 전체와 커버리지

Refs #686
Refs #694

🤖 Generated with Claude Code

The Turbopack source and CSS loaders each had their own coordinator HTTP
client. The CSS loader cached one port for every port file, so a second app
in the same process fetched the first app's CSS. A loader whose coordinator
restarted failed the build instead of reading the port the new coordinator
published. Neither client set a timeout, so a coordinator that accepted a
request and then stopped answering hung the build forever. A port file read
while it was being written was parsed as `NaN` and cached.

Both loaders now share one client that:
- caches the port per port file and ignores content that is not a valid port
  until the file is complete (an error names the file if it never is);
- on a refused connection forgets the port, reads the file again, and sends
  the request once to a new port, reporting the coordinator as unreachable if
  the file still names the same one;
- fails a request that gets no response for 300s, well above the longest wait
  the coordinator makes on purpose.

Refs #686 (NXT-04, NXT-05, NXT-06), #694 (NXT-16)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
packages/next-plugin/src/coordinator-client.ts 100.00% <100.00%> (ø)
packages/next-plugin/src/css-loader.ts 100.00% <100.00%> (ø)
packages/next-plugin/src/loader.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@owjs3901
owjs3901 merged commit 9966ecb into main Nov 17, 2026
52 of 53 checks passed
@owjs3901
owjs3901 deleted the fix/next-coordinator-client branch November 17, 2026 04:25
owjs3901 added a commit that referenced this pull request Nov 17, 2026
css-loader.ts now reaches the coordinator through callCoordinator (#917);
keep this branch's status-plus-reason error message on top of reply.body.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
owjs3901 added a commit that referenced this pull request Nov 17, 2026
coordinator.ts: main's Turbopack theme reload (#944) kept its watch in
module globals; it now lives in each app's CoordinatorState, created
from options.theme, and refreshTheme(state) runs before each /css and
/extract of that app. The reload registers the theme through the app's
session compiler, so one app's devup.json never reaches another's
engine. close() writes the pending files synchronously (#917) before
releasing the app, and resetCoordinator keeps clearing the registry
instead of the removed globals. extractor/lib.rs keeps both the
ExtractorCaches export and vanilla_extract_theme_import_tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant