[PR #5442] [MERGED] fix(xray): guard log-writer race and bound handler gRPC deadlines #5884

Closed
opened 2026-11-02 04:04:46 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5442
Author: @n0ctal
Created: 6/19/2026
Status: ✅ Merged
Merged: 6/20/2026
Merged by: @MHSanaei

Base: main ← Head: pr-h09h11-xray-hardening


📝 Commits (3)

  • b906689 perf(xray): compile log/traffic regexps once at package scope
  • de772d8 fix(xray): guard LogWriter.lastLine against the GetResult reader race
  • b10adb8 fix(xray): bound handler gRPC calls with a deadline

📊 Changes

4 files changed (+99 additions, -17 deletions)

View changed files

📝 internal/xray/api.go (+30 -6)
📝 internal/xray/log_writer.go (+30 -9)
➕ internal/xray/log_writer_race_test.go (+36 -0)
📝 internal/xray/process.go (+3 -2)

📄 Description

Stacked on top of #5362 (perf(xray): compile log/traffic regexps once at package scope).
Both fixes here live in the same two files that #5362 edits (internal/xray/log_writer.go,
internal/xray/api.go), so this branch is written on top of #5362 to avoid a merge
conflict between the two PRs. Please merge #5362 first. Once it lands, the diff here is
just the two fixes below; until then the diff also shows #5362's regexp hoist as context.

Summary

Two small, independent correctness fixes to the Xray gRPC/log internals: a data race on the
log writer's last line, and missing deadlines on handler gRPC calls.

Why

  • LogWriter.lastLine data race. Write is driven by the Xray process output goroutine,
    while Process.GetResult reads lastLine from the caller's goroutine. The field is
    unsynchronized, so the concurrent read/write is a data race (go test -race).
  • Handler gRPC calls have no deadline. AddInbound, DelInbound and the AddUser ->
    AlterInbound call use context.Background(). If the core connection hangs, the caller
    blocks indefinitely — especially bad while the process restart lock is held. The other
    handler operations already use a 10s timeout; these three were missed.

Scope

  • log_writer.go: add an RWMutex to LogWriter, expose LastLine(), and route every write
    through setLastLine().
  • process.go: GetResult now reads the last line via LastLine().
  • api.go: add handlerRPCTimeout (10s) and apply it (plus a nil-client guard) to
    AddInbound, DelInbound, and the AddUser AlterInbound call.

Validation

  • go build ./internal/xray/... and go vet ./internal/xray/... are clean.
  • go test -race -shuffle=on ./internal/xray/... passes, including the added
    TestLogWriterLastLineConcurrent (concurrent Write + LastLine).

Risk

Low. Both changes are localized to the Xray internals; behavior is unchanged except that
concurrent access to lastLine is serialized and the three handler RPCs are now bounded.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/5442 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 6/19/2026 **Status:** ✅ Merged **Merged:** 6/20/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `pr-h09h11-xray-hardening` --- ### 📝 Commits (3) - [`b906689`](https://github.com/MHSanaei/3x-ui/commit/b90668997e7b3b747dca65b9c02465ea0fe8fa1b) perf(xray): compile log/traffic regexps once at package scope - [`de772d8`](https://github.com/MHSanaei/3x-ui/commit/de772d85715471a057e1ca430e3488750bf0caa4) fix(xray): guard LogWriter.lastLine against the GetResult reader race - [`b10adb8`](https://github.com/MHSanaei/3x-ui/commit/b10adb8cce1e23bd6e838e1def6e813a88d4ac59) fix(xray): bound handler gRPC calls with a deadline ### 📊 Changes **4 files changed** (+99 additions, -17 deletions) <details> <summary>View changed files</summary> 📝 `internal/xray/api.go` (+30 -6) 📝 `internal/xray/log_writer.go` (+30 -9) ➕ `internal/xray/log_writer_race_test.go` (+36 -0) 📝 `internal/xray/process.go` (+3 -2) </details> ### 📄 Description > **Stacked on top of #5362** (`perf(xray): compile log/traffic regexps once at package scope`). > Both fixes here live in the same two files that #5362 edits (`internal/xray/log_writer.go`, > `internal/xray/api.go`), so this branch is written **on top of #5362** to avoid a merge > conflict between the two PRs. **Please merge #5362 first.** Once it lands, the diff here is > just the two fixes below; until then the diff also shows #5362's regexp hoist as context. ## Summary Two small, independent correctness fixes to the Xray gRPC/log internals: a data race on the log writer's last line, and missing deadlines on handler gRPC calls. ## Why - **`LogWriter.lastLine` data race.** `Write` is driven by the Xray process output goroutine, while `Process.GetResult` reads `lastLine` from the caller's goroutine. The field is unsynchronized, so the concurrent read/write is a data race (`go test -race`). - **Handler gRPC calls have no deadline.** `AddInbound`, `DelInbound` and the `AddUser` -> `AlterInbound` call use `context.Background()`. If the core connection hangs, the caller blocks indefinitely — especially bad while the process restart lock is held. The other handler operations already use a 10s timeout; these three were missed. ## Scope - `log_writer.go`: add an `RWMutex` to `LogWriter`, expose `LastLine()`, and route every write through `setLastLine()`. - `process.go`: `GetResult` now reads the last line via `LastLine()`. - `api.go`: add `handlerRPCTimeout` (10s) and apply it (plus a nil-client guard) to `AddInbound`, `DelInbound`, and the `AddUser` `AlterInbound` call. ## Validation - `go build ./internal/xray/...` and `go vet ./internal/xray/...` are clean. - `go test -race -shuffle=on ./internal/xray/...` passes, including the added `TestLogWriterLastLineConcurrent` (concurrent `Write` + `LastLine`). ## Risk Low. Both changes are localized to the Xray internals; behavior is unchanged except that concurrent access to `lastLine` is serialized and the three handler RPCs are now bounded. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:04:46 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#5884
No description provided.