Skip to content

TUN inbound: Duplicate the iOS tunnel file descriptor - #6883

Merged
RPRX merged 1 commit into
XTLS:mainfrom
hossinasaadi:patch-ios-tun
Oct 8, 2026
Merged

RPRX merged 1 commit into
XTLS:mainfrom
hossinasaadi:patch-ios-tun

Conversation

@hossinasaadi

Copy link
Copy Markdown
Contributor

On iOS the tun fd belongs to NetworkExtension, and the app reuses it every time the core restarts (wake from sleep, reload). Xray wrapped it in an os.File, so once the core stopped, Go GC closed it, and after the next restart the tunnel either had no fd or quietly stopped passing traffic.

Duplicating the fd lets Xray close its own copy without ever touching the one iOS owns.

@Fangliding

Copy link
Copy Markdown
Member

syscall.Dup() ?

@hossinasaadi

Copy link
Copy Markdown
Contributor Author

syscall.Dup() ?

syscall.Dup() would work, but F_DUPFD_CLOEXEC also sets close-on-exec atomically.
change it to dup?

@svlavr

svlavr commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

ownsFd should probably be renamed too. After dup, Xray owns the fd anyway, so the name is misleading. managesRoutes would fit better

@Fangliding

Fangliding commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

syscall.Dup() would work, but F_DUPFD_CLOEXEC also sets close-on-exec atomically.
change it to dup?

not need if so

@RPRX

RPRX commented Oct 5, 2026

Copy link
Copy Markdown
Member

@yiguodev 你看看

@yiguodev

yiguodev commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

Reviewed commit 9929c7c. The fix looks correct to me; I found no blocking issue.

Skipping an explicit close is insufficient when a borrowed descriptor is wrapped in os.File: its finalizer can still close the original descriptor after GC. Duplicating the descriptor before wrapping it gives Xray its own handle to close, while preserving the NetworkExtension-owned handle. The SetNonblock failure path correctly releases the duplicate, and externally managed interfaces still skip system route configuration. Keeping F_DUPFD_CLOEXEC is reasonable.

I validated this locally on macOS using a socketpair, without starting a VPN:

  • Reproduced GC closing the original descriptor with the old wrapping approach.
  • Exercised the PR's actual supplied-fd NewTun path through 100 create/start/close/GC cycles. The original descriptor remained usable for I/O after every cycle.
  • The observed open-fd count remained 6 before and after those cycles (scanned range: 0–1023).

The ownsFd naming concern is non-blocking. After duplication, Xray owns its descriptor in both paths, while the flag now controls system route management. Renaming it would improve clarity, but leaving it unchanged does not break the current behavior. It should not simply be set to true in the supplied-fd path.

Real NetworkExtension sleep/wake and reload scenarios were not exercised. Adding a regression test for borrowed-fd close/GC/reuse would be useful.

@yiguodev

yiguodev commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

patterniha pushed a commit to patterniha/Xray-core that referenced this pull request Oct 6, 2026
Early merge of XTLS#6883 (PR head 9929c7c), squashed, ahead of
it landing upstream: XTLS#6883
@RPRX RPRX changed the title TUN inbound: duplicate the iOS tunnel file descriptor TUN inbound: Duplicate the iOS tunnel file descriptor Oct 8, 2026
@RPRX
RPRX merged commit 1a60fc7 into XTLS:main Oct 8, 2026
40 checks passed
fanymagnet pushed a commit to fanymagnet/Xray-core that referenced this pull request Oct 8, 2026
@hossinasaadi
hossinasaadi deleted the patch-ios-tun branch October 9, 2026 16:55
patterniha added a commit that referenced this pull request Oct 29, 2026
…nt changes in README

Covers #7173 (a connection that cannot be bound fails
instead of leaving unbound), #7214 (IPv4-only and IPv6-only outbound
interfaces on Windows), #7230 (IPV6_BOUND_IF on macOS), and also #7130
(built-in TCP/IP stack), #6874 (IPv6 disabled system-wide on Windows),
#6811 (an existing Wintun adapter is reused) and #6883 (the iOS fd is
duplicated). #7092 and autoSystemWfpBlockLeak were already covered.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

5 participants