From 74a7b6f61839388b8e1f577210a92a89acb1be4b Mon Sep 17 00:00:00 2001 From: Jonathan Flat Date: Wed, 30 Sep 2026 15:59:05 -0600 Subject: [PATCH 1/6] Fix missing `done` in IORing.blockingConsumeCompletions --- Sources/System/IORing/IORing.swift | 2 +- Tests/SystemTests/IORingTests.swift | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Sources/System/IORing/IORing.swift b/Sources/System/IORing/IORing.swift index 2426048..c55f28a 100644 --- a/Sources/System/IORing/IORing.swift +++ b/Sources/System/IORing/IORing.swift @@ -584,8 +584,8 @@ public struct IORing: ~Copyable { break } } - try consumer(nil, nil, true) } + try consumer(nil, nil, true) } @inlinable diff --git a/Tests/SystemTests/IORingTests.swift b/Tests/SystemTests/IORingTests.swift index d843015..799e591 100644 --- a/Tests/SystemTests/IORingTests.swift +++ b/Tests/SystemTests/IORingTests.swift @@ -286,6 +286,17 @@ final class IORingTests: XCTestCase { XCTAssertGreaterThanOrEqual(elapsed, .milliseconds(50)) } + func testBlockingConsumeCompletionsSendsDone() throws { + try XCTSkipIf(!uringEnabled, failureMessage) + var ring = try IORing(queueDepth: 4) + XCTAssertTrue(try ring.submit(linkedRequests: .nop())) + var dones: [Bool] = [] + ring.blockingConsumeCompletions { (completion: consuming IORing.Completion?, error, done) in + dones.append(done) + } + XCTAssertEqual(dones, [false, true]) + } + func testRegisterEventFDTwiceThrows() throws { try XCTSkipIf(!uringEnabled, failureMessage) var ring = try IORing(queueDepth: 4) From cbf57d457533d4f55b6ca6a7530643e2df9f4b0d Mon Sep 17 00:00:00 2001 From: Franz Busch Date: Mon, 5 Oct 2026 20:58:32 +0200 Subject: [PATCH 2/6] [IO_URING] Keep the completion of failed IORing operations When an operation failed, the blocking consume methods dropped its completion and only reported the `Errno`. The caller could not tell which request failed, since the `context` was lost. This breaks any user that matches completions to requests. This PR makes `blockingConsumeCompletions` pass the completion of a failed operation together with its error. Errors of the ring itself, such as `.timeout`, are still passed without a completion. Additionally, `blockingConsumeCompletion(timeout:)` now returns the completion of a failed operation instead of throwing. It only throws errors of the ring itself. I also added `Completion.error`, which is `nil` on success and the decoded `Errno` otherwise to make it easy to get to the error of a completion. Fixes #364 Fixes #395 --- Sources/System/IORing/IOCompletion.swift | 12 +++++-- Sources/System/IORing/IORequest.swift | 6 ++-- Sources/System/IORing/IORing.swift | 44 ++++++++++++++---------- Tests/SystemTests/IORingTests.swift | 44 ++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 24 deletions(-) diff --git a/Sources/System/IORing/IOCompletion.swift b/Sources/System/IORing/IOCompletion.swift index 672606b..9c7d55d 100644 --- a/Sources/System/IORing/IOCompletion.swift +++ b/Sources/System/IORing/IOCompletion.swift @@ -60,15 +60,21 @@ public extension IORing.Completion { /// for a read or a write, an event mask for a poll, and so on. /// /// A negative value is an `errno` code multiplied by -1. Recover the error - /// by negating it again: `Errno(rawValue: -completion.result)`. Only - /// ``IORing/tryConsumeCompletion()`` returns completions with a negative - /// result; the blocking methods report the error as an ``Errno`` instead. + /// by negating it again: `Errno(rawValue: -completion.result)`, or use + /// ``error``. @inlinable var result: Int32 { get { rawValue.res } } + /// The error of the completed operation, or `nil` if the operation succeeded. + @inlinable var error: Errno? { + get { + rawValue.res < 0 ? Errno(rawValue: -rawValue.res) : nil + } + } + @inlinable var flags: IORing.Completion.Flags { get { Flags(rawValue: rawValue.flags & 0x0000FFFF) diff --git a/Sources/System/IORing/IORequest.swift b/Sources/System/IORing/IORequest.swift index a5c4c40..cc4a7f0 100644 --- a/Sources/System/IORing/IORequest.swift +++ b/Sources/System/IORing/IORequest.swift @@ -278,9 +278,9 @@ extension IORing.Request { /// } /// ``` /// - /// If the poll ends with an error, such as ``Errno/canceled``, - /// ``IORing/blockingConsumeCompletion(timeout:)`` throws that error - /// instead of returning the completion. + /// If the poll ends with an error, such as ``Errno/canceled``, its last + /// completion carries that error in ``IORing/Completion/error`` and + /// doesn't contain `.moreCompletions`. /// /// - Parameters: /// - file: The file descriptor to monitor for I/O events. diff --git a/Sources/System/IORing/IORing.swift b/Sources/System/IORing/IORing.swift index c55f28a..38cef62 100644 --- a/Sources/System/IORing/IORing.swift +++ b/Sources/System/IORing/IORing.swift @@ -519,11 +519,10 @@ public struct IORing: ~Copyable { var count = 0 while let completion = _tryConsumeCompletion(ring: completionRing) { count += 1 - if completion.result < 0 { - try consumer(nil, Errno(rawValue: -completion.result), false) - } else { - try consumer(completion, nil, false) - } + // We still return the completion for a failed operation so that the + // caller can tell which request failed through its `context`. + let error = completion.error + try consumer(completion, error, false) if count == maximumCount { try consumer(nil, nil, true) return @@ -575,11 +574,8 @@ public struct IORing: ~Copyable { var count = 0 while let completion = _tryConsumeCompletion(ring: completionRing) { count += 1 - if completion.result < 0 { - try consumer(nil, Errno(rawValue: -completion.result), false) - } else { - try consumer(completion, nil, false) - } + let error = completion.error + try consumer(completion, error, false) if count == maximumCount { break } @@ -595,17 +591,23 @@ public struct IORing: ~Copyable { var result: Completion? = nil try _blockingConsumeCompletionGuts(minimumCount: 1, maximumCount: 1, extraArgs: extraArgs) { (completion: consuming Completion?, error, done) throws(Errno) in - if let error { - throw error - } if let completion { result = consume completion + } else if let error { + throw error } } return result.take()! } /// Synchronously waits for an operation to complete for up to `timeout` (or forever if not specified) + /// + /// The completion is returned even if its operation failed. Check + /// ``IORing/Completion/error`` to find out whether it did, and + /// ``IORing/Completion/context`` to find out which request it belongs to. + /// + /// - Throws: An error of the ring itself, such as ``Errno/timeout`` when + /// no operation completed within `timeout`. @inlinable public func blockingConsumeCompletion( timeout: Duration? = nil @@ -629,7 +631,17 @@ public struct IORing: ~Copyable { } } - /// Synchronously waits for `minimumCount` or more operations to complete for up to `timeout` (or forever if not specified). For each completed operation found, `consumer` is called to handle processing it + /// Synchronously waits for `minimumCount` or more operations to complete for up to `timeout` + /// (or forever if not specified). For each completed operation found, `consumer` is called to handle + /// processing it. + /// + /// `consumer` receives a completion, an error, and whether consuming is done: + /// - For each completed operation, it receives the completion. If the + /// operation failed, it receives the operation's error alongside the + /// completion. + /// - If the ring itself fails, such as with ``Errno/timeout``, it receives + /// the error without a completion. + /// - Once consuming is done, it is called a final time with `done` set to `true`. @inlinable public func blockingConsumeCompletions( minimumCount: UInt32 = 1, @@ -658,10 +670,6 @@ public struct IORing: ~Copyable { } } - // public func peekNextCompletion() -> IOCompletion { - - // } - /// Takes a completed operation from the ring and returns it, if one is ready. Otherwise, returns nil. @inlinable public func tryConsumeCompletion() -> Completion? { diff --git a/Tests/SystemTests/IORingTests.swift b/Tests/SystemTests/IORingTests.swift index 799e591..24fba4f 100644 --- a/Tests/SystemTests/IORingTests.swift +++ b/Tests/SystemTests/IORingTests.swift @@ -297,6 +297,50 @@ final class IORingTests: XCTestCase { XCTAssertEqual(dones, [false, true]) } + func testBlockingConsumeCompletionsKeepsContextOfFailedCompletion() throws { + try XCTSkipIf(!uringEnabled, failureMessage) + var ring = try IORing(queueDepth: 4) + XCTAssertTrue(try ring.submit( + linkedRequests: .close(FileDescriptor(rawValue: -1), context: 42) + )) + + var received: [(context: UInt64?, error: Errno?, done: Bool)] = [] + ring.blockingConsumeCompletions { (completion: consuming IORing.Completion?, error, done) in + received.append((completion?.context, error, done)) + } + + XCTAssertEqual(received.count, 2) + XCTAssertEqual(received.first?.context, 42) + XCTAssertEqual(received.first?.error, .badFileDescriptor) + XCTAssertEqual(received.first?.done, false) + XCTAssertEqual(received.last?.context, nil) + XCTAssertEqual(received.last?.error, nil) + XCTAssertEqual(received.last?.done, true) + } + + func testBlockingConsumeCompletionReturnsFailedCompletion() throws { + try XCTSkipIf(!uringEnabled, failureMessage) + var ring = try IORing(queueDepth: 4) + XCTAssertTrue(try ring.submit( + linkedRequests: .close(FileDescriptor(rawValue: -1), context: 43) + )) + + let completion = try ring.blockingConsumeCompletion() + XCTAssertEqual(completion.context, 43) + XCTAssertEqual(completion.error, .badFileDescriptor) + XCTAssertEqual(completion.result, -Errno.badFileDescriptor.rawValue) + } + + func testCompletionOfSuccessfulOperationHasNoError() throws { + try XCTSkipIf(!uringEnabled, failureMessage) + var ring = try IORing(queueDepth: 4) + XCTAssertTrue(try ring.submit(linkedRequests: .nop())) + + let completion = try ring.blockingConsumeCompletion() + XCTAssertEqual(completion.result, 0) + XCTAssertNil(completion.error) + } + func testRegisterEventFDTwiceThrows() throws { try XCTSkipIf(!uringEnabled, failureMessage) var ring = try IORing(queueDepth: 4) From 95ed6aa2a4bd3d765dde19498037a21e71dbc426 Mon Sep 17 00:00:00 2001 From: Franz Busch Date: Tue, 6 Oct 2026 10:21:52 +0200 Subject: [PATCH 3/6] Address review comments --- Sources/System/IORing/IORequest.swift | 4 ++++ Sources/System/IORing/IORing.swift | 8 ++++++++ Tests/SystemTests/IORingTests.swift | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/Sources/System/IORing/IORequest.swift b/Sources/System/IORing/IORequest.swift index cc4a7f0..544c1ef 100644 --- a/Sources/System/IORing/IORequest.swift +++ b/Sources/System/IORing/IORequest.swift @@ -274,6 +274,10 @@ extension IORing.Request { /// continue /// } /// armed = completion.flags.contains(.moreCompletions) + /// if let error = completion.error { + /// // The poll ended with an error, such as `.canceled`. + /// throw error + /// } /// // Handle incoming connection /// } /// ``` diff --git a/Sources/System/IORing/IORing.swift b/Sources/System/IORing/IORing.swift index 38cef62..473fe43 100644 --- a/Sources/System/IORing/IORing.swift +++ b/Sources/System/IORing/IORing.swift @@ -818,6 +818,14 @@ public struct IORing: ~Copyable { } /// Sends all prepared requests to the kernel for processing, and then dequeues at least `minimumCount` completions, waiting up to `timeout` for them to become available. `consumer` is called to process each completed IO operation as it becomes available. + /// + /// `consumer` receives a completion, an error, and whether consuming is done: + /// - For each completed operation, it receives the completion. If the + /// operation failed, it receives the operation's error alongside the + /// completion. + /// - If submitting the requests or waiting for completions fails, such as + /// with ``Errno/timeout``, it receives the error without a completion. + /// - Once consuming is done, it is called a final time with `done` set to `true`. @inlinable public func submitPreparedRequestsAndConsumeCompletions( minimumCount: UInt32 = 1, diff --git a/Tests/SystemTests/IORingTests.swift b/Tests/SystemTests/IORingTests.swift index 24fba4f..dffe76b 100644 --- a/Tests/SystemTests/IORingTests.swift +++ b/Tests/SystemTests/IORingTests.swift @@ -331,6 +331,26 @@ final class IORingTests: XCTestCase { XCTAssertEqual(completion.result, -Errno.badFileDescriptor.rawValue) } + func testSubmitAndConsumeCompletionsKeepsContextOfFailedCompletion() throws { + try XCTSkipIf(!uringEnabled, failureMessage) + var ring = try IORing(queueDepth: 4) + XCTAssertTrue(ring.prepare(request: .close(FileDescriptor(rawValue: -1), context: 44))) + + var received: [(context: UInt64?, error: Errno?, done: Bool)] = [] + ring.submitPreparedRequestsAndConsumeCompletions { + (completion: consuming IORing.Completion?, error, done) in + received.append((completion?.context, error, done)) + } + + XCTAssertEqual(received.count, 2) + XCTAssertEqual(received.first?.context, 44) + XCTAssertEqual(received.first?.error, .badFileDescriptor) + XCTAssertEqual(received.first?.done, false) + XCTAssertEqual(received.last?.context, nil) + XCTAssertEqual(received.last?.error, nil) + XCTAssertEqual(received.last?.done, true) + } + func testCompletionOfSuccessfulOperationHasNoError() throws { try XCTSkipIf(!uringEnabled, failureMessage) var ring = try IORing(queueDepth: 4) From f5fe750a576177b99e0a4b1b24f923ba645c785d Mon Sep 17 00:00:00 2001 From: Jonathan Flat Date: Wed, 30 Sep 2026 09:06:03 -0600 Subject: [PATCH 4/6] Improve CSystem import handling --- Sources/CSystem/CMakeLists.txt | 2 - Sources/CSystem/include/CSystemDarwin.h | 20 -------- Sources/CSystem/include/CSystemLinux.h | 32 ++++++++----- Sources/CSystem/include/CSystemWindows.h | 17 ------- Sources/CSystem/include/io_uring.h | 46 ++----------------- Sources/CSystem/include/module.modulemap | 13 +++++- Sources/CSystem/shims.c | 43 +++++++++++++++-- .../System/FilePath/FilePathTempPosix.swift | 4 +- Sources/System/FileSystem/Stat.swift | 3 -- Sources/System/IORing/IOCompletion.swift | 2 +- Sources/System/IORing/IORequest.swift | 2 +- Sources/System/IORing/IORing+Util.swift | 15 +++--- Sources/System/IORing/IORing.swift | 5 +- Sources/System/IORing/RawIORequest.swift | 2 +- Sources/System/Internals/CInterop.swift | 8 +--- Sources/System/Internals/Constants.swift | 11 ++++- Sources/System/Internals/Exports.swift | 7 +-- Sources/System/Internals/Syscalls.swift | 2 +- Tests/MemberImportVisibility/Probe.swift | 7 ++- Tests/SystemTests/FileOperationsTest.swift | 16 +++++++ Tests/SystemTests/IORingTests.swift | 7 +-- Tests/SystemTests/StatTests.swift | 2 - 22 files changed, 126 insertions(+), 140 deletions(-) delete mode 100644 Sources/CSystem/include/CSystemDarwin.h delete mode 100644 Sources/CSystem/include/CSystemWindows.h diff --git a/Sources/CSystem/CMakeLists.txt b/Sources/CSystem/CMakeLists.txt index 70e08e4..49fbd6a 100644 --- a/Sources/CSystem/CMakeLists.txt +++ b/Sources/CSystem/CMakeLists.txt @@ -13,11 +13,9 @@ target_include_directories(CSystem PUBLIC $) install(FILES - include/CSystemDarwin.h include/CSystemLinux.h include/CSystemShared.h include/CSystemWASI.h - include/CSystemWindows.h include/CSystemFreeBSD.h include/io_uring.h include/module.modulemap diff --git a/Sources/CSystem/include/CSystemDarwin.h b/Sources/CSystem/include/CSystemDarwin.h deleted file mode 100644 index bcef0a6..0000000 --- a/Sources/CSystem/include/CSystemDarwin.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - This source file is part of the Swift System open source project - - Copyright (c) 2026 Apple Inc. and the Swift System project authors - Licensed under Apache License v2.0 with Runtime Library Exception - - See https://swift.org/LICENSE.txt for license information -*/ - -#if defined(__APPLE__) - -#include - -#ifdef O_CLOFORK -#define COMPATIBILITY_O_CLOFORK O_CLOFORK -#else -#define COMPATIBILITY_O_CLOFORK 0 -#endif - -#endif // defined(__APPLE__) diff --git a/Sources/CSystem/include/CSystemLinux.h b/Sources/CSystem/include/CSystemLinux.h index 815e142..3c99b69 100644 --- a/Sources/CSystem/include/CSystemLinux.h +++ b/Sources/CSystem/include/CSystemLinux.h @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2026 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -9,16 +9,24 @@ #ifdef __linux__ -#include +// Glibc doesn't export eventfd, which the tests use. #include -#include -#include -#include -#include -#include -#include -#include -#include -#include "io_uring.h" -#endif +#if !defined(__ANDROID__) +#include +#include "io_uring.h" + +// Defined in shims.c, which can include the libc headers they need. +extern int csystem_io_uring_setup(unsigned int entries, + struct io_uring_params *p); +extern int csystem_io_uring_enter(int fd, unsigned int to_submit, + unsigned int min_complete, + unsigned int flags, void *sig); +extern int csystem_io_uring_enter2(int fd, unsigned int to_submit, + unsigned int min_complete, + unsigned int flags, void *args, size_t sz); +extern int csystem_io_uring_register(int fd, unsigned int opcode, void *arg, + unsigned int nr_args); +#endif // !defined(__ANDROID__) + +#endif // __linux__ diff --git a/Sources/CSystem/include/CSystemWindows.h b/Sources/CSystem/include/CSystemWindows.h deleted file mode 100644 index 7719be9..0000000 --- a/Sources/CSystem/include/CSystemWindows.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - This source file is part of the Swift System open source project - - Copyright (c) 2020 Apple Inc. and the Swift System project authors - Licensed under Apache License v2.0 with Runtime Library Exception - - See https://swift.org/LICENSE.txt for license information -*/ - -#if defined(_WIN32) - -#define NOMINMAX -#define WIN32_LEAN_AND_MEAN -#define VC_EXTRA_LEAN -#include - -#endif diff --git a/Sources/CSystem/include/io_uring.h b/Sources/CSystem/include/io_uring.h index defc284..b633283 100644 --- a/Sources/CSystem/include/io_uring.h +++ b/Sources/CSystem/include/io_uring.h @@ -1,7 +1,6 @@ -#include -#include -#include -#include +#pragma once + +// Types only: the syscall wrappers need libc headers, so they're in shims.c. #define __SWIFT_IORING_SQE_FALLBACK_STRUCT { \ __u8 opcode; \ @@ -193,19 +192,6 @@ struct io_uring_params { }; #endif // __has_include() -#ifndef SWIFT_IORING_C_WRAPPER -#define SWIFT_IORING_C_WRAPPER - -# ifndef __NR_io_uring_setup -# define __NR_io_uring_setup 425 -# endif -# ifndef __NR_io_uring_enter -# define __NR_io_uring_enter 426 -# endif -# ifndef __NR_io_uring_register -# define __NR_io_uring_register 427 -# endif - /* struct io_uring_getevents_arg { __u64 sigmask; @@ -220,29 +206,3 @@ struct swift_io_uring_getevents_arg { __u32 min_wait_usec; __u64 ts; }; - -static inline int io_uring_register(int fd, unsigned int opcode, void *arg, - unsigned int nr_args) -{ - return syscall(__NR_io_uring_register, fd, opcode, arg, nr_args); -} - -static inline int io_uring_setup(unsigned int entries, struct io_uring_params *p) -{ - return syscall(__NR_io_uring_setup, entries, p); -} - -static inline int io_uring_enter2(int fd, unsigned int to_submit, unsigned int min_complete, - unsigned int flags, void *args, size_t sz) -{ - return syscall(__NR_io_uring_enter, fd, to_submit, min_complete, - flags, args, sz); -} - -static inline int io_uring_enter(int fd, unsigned int to_submit, unsigned int min_complete, - unsigned int flags, sigset_t *sig) -{ - return io_uring_enter2(fd, to_submit, min_complete, flags, sig, _NSIG / 8); -} - -#endif diff --git a/Sources/CSystem/include/module.modulemap b/Sources/CSystem/include/module.modulemap index 31e1371..07a52bf 100644 --- a/Sources/CSystem/include/module.modulemap +++ b/Sources/CSystem/include/module.modulemap @@ -1,9 +1,18 @@ +// System imports CSystem with `internal import`. Don't include a header that +// the platform's C module also includes: CSystem would then co-own its +// declarations, so Swift would limit them to CSystem's import access level and +// could credit their members to CSystem under MemberImportVisibility. +// (CSystemWASI.h and CSystemFreeBSD.h still do; see Constants.swift.) module CSystem { header "CSystemShared.h" - header "CSystemDarwin.h" header "CSystemLinux.h" header "CSystemWASI.h" - header "CSystemWindows.h" header "CSystemFreeBSD.h" export * } + +// IORing's inlinable code stores these types, so System imports this module +// with `public import`. Keep libc headers out of it too. +module CSystemIOUring { + header "io_uring.h" +} diff --git a/Sources/CSystem/shims.c b/Sources/CSystem/shims.c index b32bd84..9ba0eb2 100644 --- a/Sources/CSystem/shims.c +++ b/Sources/CSystem/shims.c @@ -20,13 +20,12 @@ #ifdef __linux__ #define _GNU_SOURCE +#include +#include +#include #include #endif -#if defined(_WIN32) -#include -#endif - #include #if !defined(_WIN32) && !defined(__wasi__) && !defined(__APPLE__) @@ -70,3 +69,39 @@ extern int csystem_posix_dup3(int fildes, int fildes2, int flag) { return -1; #endif } + +#if defined(__linux__) && !defined(__ANDROID__) +# ifndef __NR_io_uring_setup +# define __NR_io_uring_setup 425 +# endif +# ifndef __NR_io_uring_enter +# define __NR_io_uring_enter 426 +# endif +# ifndef __NR_io_uring_register +# define __NR_io_uring_register 427 +# endif + +extern int csystem_io_uring_setup(unsigned int entries, + struct io_uring_params *p) { + return syscall(__NR_io_uring_setup, entries, p); +} + +extern int csystem_io_uring_enter2(int fd, unsigned int to_submit, + unsigned int min_complete, + unsigned int flags, void *args, size_t sz) { + return syscall(__NR_io_uring_enter, fd, to_submit, min_complete, flags, + args, sz); +} + +extern int csystem_io_uring_enter(int fd, unsigned int to_submit, + unsigned int min_complete, + unsigned int flags, void *sig) { + return csystem_io_uring_enter2(fd, to_submit, min_complete, flags, sig, + _NSIG / 8); +} + +extern int csystem_io_uring_register(int fd, unsigned int opcode, void *arg, + unsigned int nr_args) { + return syscall(__NR_io_uring_register, fd, opcode, arg, nr_args); +} +#endif // defined(__linux__) && !defined(__ANDROID__) diff --git a/Sources/System/FilePath/FilePathTempPosix.swift b/Sources/System/FilePath/FilePathTempPosix.swift index 2ac7098..8b548a8 100644 --- a/Sources/System/FilePath/FilePathTempPosix.swift +++ b/Sources/System/FilePath/FilePathTempPosix.swift @@ -12,15 +12,13 @@ #if SYSTEM_PACKAGE_DARWIN import Darwin #elseif canImport(Glibc) -import CSystem import Glibc #elseif canImport(Musl) -import CSystem import Musl #elseif canImport(WASILibc) +internal import CSystem // For the members of system_dirent import WASILibc #elseif canImport(Android) -import CSystem import Android #else #error("Unsupported Platform") diff --git a/Sources/System/FileSystem/Stat.swift b/Sources/System/FileSystem/Stat.swift index ce4b6b5..e4a7f9d 100644 --- a/Sources/System/FileSystem/Stat.swift +++ b/Sources/System/FileSystem/Stat.swift @@ -15,15 +15,12 @@ #if SYSTEM_PACKAGE_DARWIN import Darwin #elseif canImport(Glibc) -import CSystem import Glibc #elseif canImport(Musl) -import CSystem import Musl #elseif canImport(WASILibc) import WASILibc #elseif canImport(Android) -import CSystem import Android #else #error("Unsupported Platform") diff --git a/Sources/System/IORing/IOCompletion.swift b/Sources/System/IORing/IOCompletion.swift index d9e6905..53588b0 100644 --- a/Sources/System/IORing/IOCompletion.swift +++ b/Sources/System/IORing/IOCompletion.swift @@ -1,7 +1,7 @@ #if compiler(>=6.2) && $Lifetimes #if os(Linux) -import CSystem +public import CSystemIOUring public extension IORing { struct Completion: ~Copyable { diff --git a/Sources/System/IORing/IORequest.swift b/Sources/System/IORing/IORequest.swift index 4a388c4..b23a843 100644 --- a/Sources/System/IORing/IORequest.swift +++ b/Sources/System/IORing/IORequest.swift @@ -1,7 +1,7 @@ #if compiler(>=6.2) && $Lifetimes #if os(Linux) -import CSystem +public import CSystemIOUring @usableFromInline internal enum IORequestCore { diff --git a/Sources/System/IORing/IORing+Util.swift b/Sources/System/IORing/IORing+Util.swift index be25886..2ff757c 100644 --- a/Sources/System/IORing/IORing+Util.swift +++ b/Sources/System/IORing/IORing+Util.swift @@ -10,14 +10,15 @@ #if compiler(>=6.2) && $Lifetimes #if os(Linux) -import CSystem +internal import CSystem +public import CSystemIOUring #if canImport(Glibc) import Glibc #elseif canImport(Musl) import Musl #endif -/// Throwing wrapper around the `io_uring_enter2` shim. +/// Throwing wrapper around the `csystem_io_uring_enter2` shim. /// /// On success returns the syscall's non-negative result (the number of SQEs /// consumed by the kernel). On failure, reads `errno` and throws the matching @@ -32,14 +33,14 @@ internal func _ioUringEnter2( argsSize: Int ) throws(Errno) -> Int32 { let result = valueOrErrno(retryOnInterrupt: true) { - io_uring_enter2( + csystem_io_uring_enter2( ringDescriptor, toSubmit, minComplete, flags, args, argsSize ) } return try result.get() } -/// Throwing wrapper around the `io_uring_enter` shim. +/// Throwing wrapper around the `csystem_io_uring_enter` shim. /// /// On success returns the syscall's non-negative result (the number of SQEs /// consumed by the kernel). On failure, reads `errno` and throws the matching @@ -53,12 +54,12 @@ internal func _ioUringEnter( sig: UnsafeMutablePointer? ) throws(Errno) -> Int32 { let result = valueOrErrno(retryOnInterrupt: true) { - io_uring_enter(ringDescriptor, toSubmit, minComplete, flags, sig) + csystem_io_uring_enter(ringDescriptor, toSubmit, minComplete, flags, sig) } return try result.get() } -/// Throwing wrapper around the `io_uring_register` shim. +/// Throwing wrapper around the `csystem_io_uring_register` shim. /// /// On success returns the syscall's non-negative result. On failure, reads /// `errno` and throws the matching `Errno`. `EINTR` is retried automatically. @@ -70,7 +71,7 @@ internal func _ioUringRegister( nrArgs: UInt32 ) throws(Errno) -> Int32 { let result = valueOrErrno(retryOnInterrupt: true) { - io_uring_register(ringDescriptor, opcode, arg, nrArgs) + csystem_io_uring_register(ringDescriptor, opcode, arg, nrArgs) } return try result.get() } diff --git a/Sources/System/IORing/IORing.swift b/Sources/System/IORing/IORing.swift index 27d27a0..bce65f5 100644 --- a/Sources/System/IORing/IORing.swift +++ b/Sources/System/IORing/IORing.swift @@ -1,7 +1,8 @@ #if compiler(>=6.2) && $Lifetimes #if os(Linux) -import CSystem +internal import CSystem +public import CSystemIOUring // needed for mmap #if canImport(Glibc) import Glibc @@ -223,7 +224,7 @@ private func setUpRing( var err: Errno? = nil let ringDescriptor = withUnsafeMutablePointer(to: ¶ms) { - let result = io_uring_setup(queueDepth, $0) + let result = csystem_io_uring_setup(queueDepth, $0) if result < 0 { err = Errno.current } diff --git a/Sources/System/IORing/RawIORequest.swift b/Sources/System/IORing/RawIORequest.swift index 40156dd..357bc98 100644 --- a/Sources/System/IORing/RawIORequest.swift +++ b/Sources/System/IORing/RawIORequest.swift @@ -1,7 +1,7 @@ #if compiler(>=6.2) && $Lifetimes #if os(Linux) -import CSystem +public import CSystemIOUring #if canImport(Glibc) import Glibc #elseif canImport(Musl) diff --git a/Sources/System/Internals/CInterop.swift b/Sources/System/Internals/CInterop.swift index 5570d1e..6d98f12 100644 --- a/Sources/System/Internals/CInterop.swift +++ b/Sources/System/Internals/CInterop.swift @@ -10,19 +10,15 @@ #if SYSTEM_PACKAGE_DARWIN import Darwin #elseif os(Windows) -import CSystem import ucrt #elseif canImport(Glibc) -import CSystem import Glibc #elseif canImport(Musl) -import CSystem import Musl #elseif canImport(WASILibc) import WASILibc -#elseif canImport(Bionic) -import CSystem -import Bionic +#elseif canImport(Android) +import Android #else #error("Unsupported Platform") #endif diff --git a/Sources/System/Internals/Constants.swift b/Sources/System/Internals/Constants.swift index 656e2c3..2a3989f 100644 --- a/Sources/System/Internals/Constants.swift +++ b/Sources/System/Internals/Constants.swift @@ -15,6 +15,7 @@ import Darwin #elseif os(Windows) import ucrt +import WinSDK #elseif canImport(Glibc) import Glibc #elseif canImport(Musl) @@ -27,7 +28,11 @@ import Android #error("Unsupported Platform") #endif -import CSystem +// Some WASI and FreeBSD constants come from CSystem and are inlined into +// clients. +#if os(WASI) || os(FreeBSD) +public import CSystem +#endif // MARK: errno #if SYSTEM_PACKAGE_DARWIN @@ -629,7 +634,9 @@ internal var _O_CLOEXEC: CInt { #if !os(Windows) @_alwaysEmitIntoClient internal var _O_CLOFORK: CInt { - #if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) + #if SYSTEM_PACKAGE_DARWIN + 0x0800_0000 // O_CLOFORK, which SDKs before macOS 27 lack + #elseif os(FreeBSD) COMPATIBILITY_O_CLOFORK #elseif !os(WASI) && !os(Linux) && !os(Android) O_CLOFORK diff --git a/Sources/System/Internals/Exports.swift b/Sources/System/Internals/Exports.swift index 6ce7d4d..7dd9d4e 100644 --- a/Sources/System/Internals/Exports.swift +++ b/Sources/System/Internals/Exports.swift @@ -10,23 +10,18 @@ // Internal wrappers and typedefs which help reduce #if littering in System's // code base. -// TODO: Should CSystem just include all the header files we need? - #if SYSTEM_PACKAGE_DARWIN import Darwin #elseif os(Windows) -import CSystem import ucrt +import WinSDK #elseif canImport(Glibc) -import CSystem import Glibc #elseif canImport(Musl) -import CSystem import Musl #elseif canImport(WASILibc) import WASILibc #elseif canImport(Android) -import CSystem import Android #else #error("Unsupported Platform") diff --git a/Sources/System/Internals/Syscalls.swift b/Sources/System/Internals/Syscalls.swift index b0d9dae..0909029 100644 --- a/Sources/System/Internals/Syscalls.swift +++ b/Sources/System/Internals/Syscalls.swift @@ -7,7 +7,7 @@ See https://swift.org/LICENSE.txt for license information */ -import CSystem +internal import CSystem #if SYSTEM_PACKAGE_DARWIN import Darwin #elseif canImport(Glibc) diff --git a/Tests/MemberImportVisibility/Probe.swift b/Tests/MemberImportVisibility/Probe.swift index a27c836..9359931 100644 --- a/Tests/MemberImportVisibility/Probe.swift +++ b/Tests/MemberImportVisibility/Probe.swift @@ -16,8 +16,9 @@ // - Access raw C members of vended types (e.g. CInterop.Stat) as a downstream // consumer would. Don't go through the Swift (e.g. Stat) wrapper since its // accesses happen inside System, where importing CSystem is fine. -// - Touch every direct member the public API exposes. One field is not always -// a safe proxy for the rest. +// - Touch every direct member the public API exposes, and the members of C +// types it exposes through them (e.g. timespec). One field is not always a +// safe proxy for the rest. #if canImport(Glibc) || canImport(Musl) || canImport(Android) #if canImport(Glibc) @@ -43,5 +44,7 @@ func _mivProbe(_ s: CInterop.Stat) { _ = s.st_atim _ = s.st_mtim _ = s.st_ctim + _ = s.st_atim.tv_sec + _ = s.st_atim.tv_nsec } #endif diff --git a/Tests/SystemTests/FileOperationsTest.swift b/Tests/SystemTests/FileOperationsTest.swift index f62dc48..2434bb5 100644 --- a/Tests/SystemTests/FileOperationsTest.swift +++ b/Tests/SystemTests/FileOperationsTest.swift @@ -201,6 +201,22 @@ final class FileOperationsTest: XCTestCase { } } } + + #if canImport(Darwin, _version: 428) // The macOS 27 SDK defines O_CLOFORK. + func testAdHocPipeCloseOnFork() throws { + guard #available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *) else { + throw XCTSkip("pipe2 and PipeOptions require anyAppleOS 27 or newer") + } + // System spells O_CLOFORK as a literal on Darwin. + XCTAssertEqual(FileDescriptor.PipeOptions.closeOnFork.rawValue, O_CLOFORK) + let pipe = try FileDescriptor.pipe(options: .closeOnFork) + try pipe.readEnd.closeAfter { + try pipe.writeEnd.closeAfter { + XCTAssertEqual(fcntl(pipe.readEnd.rawValue, F_GETFD) & FD_CLOFORK, FD_CLOFORK) + } + } + } + #endif #endif // !os(WASI) #if !os(Windows) diff --git a/Tests/SystemTests/IORingTests.swift b/Tests/SystemTests/IORingTests.swift index 97bf86c..4cd2ea9 100644 --- a/Tests/SystemTests/IORingTests.swift +++ b/Tests/SystemTests/IORingTests.swift @@ -2,7 +2,8 @@ #if os(Linux) import XCTest -import CSystem //for eventfd +import CSystem // for eventfd and csystem_io_uring_setup +import CSystemIOUring #if canImport(Glibc) import Glibc // for errno #elseif canImport(Musl) @@ -28,7 +29,7 @@ let uringEnabled: Bool = { func isUringEnabled() throws -> Bool { // Even if the kernel supports io_uring, the SystemPackage build may have // been compiled against older kernel headers that lack features it needs - // (gated on IORING_TIMEOUT_BOOTTIME in CSystem); in that configuration + // (gated on IORING_TIMEOUT_BOOTTIME in io_uring.h); in that configuration // IORing.init throws ENOTSUP. Treat that as disabled so tests skip cleanly // instead of failing. do throws(Errno) { @@ -59,7 +60,7 @@ func isUringEnabled() throws -> Bool { // - EPERM or EACCES -> treat as disabled // - propagate any other error. var params = io_uring_params() - let raw = io_uring_setup(1, ¶ms) + let raw = csystem_io_uring_setup(1, ¶ms) if raw < 0 { let err = Errno(rawValue: errno) switch err { diff --git a/Tests/SystemTests/StatTests.swift b/Tests/SystemTests/StatTests.swift index 11876b8..37e2aa2 100644 --- a/Tests/SystemTests/StatTests.swift +++ b/Tests/SystemTests/StatTests.swift @@ -16,10 +16,8 @@ import Testing #if SYSTEM_PACKAGE_DARWIN import Darwin #elseif canImport(Glibc) -import CSystem import Glibc #elseif canImport(Musl) -import CSystem import Musl #elseif canImport(WASILibc) import CSystem From 4179b2cce50ec130857543bab03ded8952ef34b4 Mon Sep 17 00:00:00 2001 From: Jonathan Flat Date: Mon, 12 Oct 2026 19:33:46 -0600 Subject: [PATCH 5/6] Remove CSystemFreeBSD.h, too --- Sources/CSystem/CMakeLists.txt | 1 - Sources/CSystem/include/CSystemFreeBSD.h | 20 -------------------- Sources/CSystem/include/module.modulemap | 3 +-- Sources/CSystem/shims.c | 4 ---- Sources/System/Internals/Constants.swift | 11 ++++------- 5 files changed, 5 insertions(+), 34 deletions(-) delete mode 100644 Sources/CSystem/include/CSystemFreeBSD.h diff --git a/Sources/CSystem/CMakeLists.txt b/Sources/CSystem/CMakeLists.txt index 49fbd6a..7f0a79f 100644 --- a/Sources/CSystem/CMakeLists.txt +++ b/Sources/CSystem/CMakeLists.txt @@ -16,7 +16,6 @@ install(FILES include/CSystemLinux.h include/CSystemShared.h include/CSystemWASI.h - include/CSystemFreeBSD.h include/io_uring.h include/module.modulemap DESTINATION include/CSystem) diff --git a/Sources/CSystem/include/CSystemFreeBSD.h b/Sources/CSystem/include/CSystemFreeBSD.h deleted file mode 100644 index d90c343..0000000 --- a/Sources/CSystem/include/CSystemFreeBSD.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - This source file is part of the Swift System open source project - - Copyright (c) 2026 Apple Inc. and the Swift System project authors - Licensed under Apache License v2.0 with Runtime Library Exception - - See https://swift.org/LICENSE.txt for license information -*/ - -#if defined(__FreeBSD__) - -#include - -#ifdef O_CLOFORK -#define COMPATIBILITY_O_CLOFORK O_CLOFORK -#else -#define COMPATIBILITY_O_CLOFORK 0 -#endif - -#endif // defined(__FreeBSD__) diff --git a/Sources/CSystem/include/module.modulemap b/Sources/CSystem/include/module.modulemap index 07a52bf..64a5c41 100644 --- a/Sources/CSystem/include/module.modulemap +++ b/Sources/CSystem/include/module.modulemap @@ -2,12 +2,11 @@ // the platform's C module also includes: CSystem would then co-own its // declarations, so Swift would limit them to CSystem's import access level and // could credit their members to CSystem under MemberImportVisibility. -// (CSystemWASI.h and CSystemFreeBSD.h still do; see Constants.swift.) +// (CSystemWASI.h still does; see Constants.swift.) module CSystem { header "CSystemShared.h" header "CSystemLinux.h" header "CSystemWASI.h" - header "CSystemFreeBSD.h" export * } diff --git a/Sources/CSystem/shims.c b/Sources/CSystem/shims.c index 9ba0eb2..b0425ad 100644 --- a/Sources/CSystem/shims.c +++ b/Sources/CSystem/shims.c @@ -14,10 +14,6 @@ #include #endif -#if defined(__FreeBSD__) -#include -#endif - #ifdef __linux__ #define _GNU_SOURCE #include diff --git a/Sources/System/Internals/Constants.swift b/Sources/System/Internals/Constants.swift index 2a3989f..f74d784 100644 --- a/Sources/System/Internals/Constants.swift +++ b/Sources/System/Internals/Constants.swift @@ -28,9 +28,8 @@ import Android #error("Unsupported Platform") #endif -// Some WASI and FreeBSD constants come from CSystem and are inlined into -// clients. -#if os(WASI) || os(FreeBSD) +// Some WASI constants come from CSystem and are inlined into clients. +#if os(WASI) public import CSystem #endif @@ -634,10 +633,8 @@ internal var _O_CLOEXEC: CInt { #if !os(Windows) @_alwaysEmitIntoClient internal var _O_CLOFORK: CInt { - #if SYSTEM_PACKAGE_DARWIN - 0x0800_0000 // O_CLOFORK, which SDKs before macOS 27 lack - #elseif os(FreeBSD) - COMPATIBILITY_O_CLOFORK + #if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) + 0x0800_0000 // O_CLOFORK, which SDKs before macOS 27 and older FreeBSD lack #elseif !os(WASI) && !os(Linux) && !os(Android) O_CLOFORK #else From d0f7d75929dc606153589e9987788136f802ad51 Mon Sep 17 00:00:00 2001 From: Jonathan Flat Date: Fri, 16 Oct 2026 17:12:07 -0600 Subject: [PATCH 6/6] Add automerger workflow from release/1.8.x to main --- .github/workflows/automerge_to_main.yml | 20 ++++++++++++++++++++ .github/workflows/pull_request.yml | 2 +- README.md | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/automerge_to_main.yml diff --git a/.github/workflows/automerge_to_main.yml b/.github/workflows/automerge_to_main.yml new file mode 100644 index 0000000..ec295ff --- /dev/null +++ b/.github/workflows/automerge_to_main.yml @@ -0,0 +1,20 @@ +name: Create PR to merge release branch into the main branch +# Changes should land on the earliest release branch they will need to ship on and then be automatically forwarded to main +permissions: + contents: read +on: + schedule: + - cron: '0 9 * * *' + workflow_dispatch: +jobs: + create_merge_pr: + name: Create PR to merge release branch into main branch + uses: swiftlang/github-workflows/.github/workflows/create_automerge_pr.yml@0.0.15 + with: + base_branch: main + head_branch: release/1.8.x + pr_message: This PR was automatically opened by a GitHub action. Review the changes included in this PR and determine if they should be included on main. If yes, merge the PR. Otherwise revert changes that should not be included on this branch. + permissions: + contents: write + pull-requests: write + if: (github.event_name == 'schedule' && github.repository == 'apple/swift-system') || (github.event_name != 'schedule') # Ensure that we don't run this on a schedule in a fork diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index ca6fbea..88c5e25 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -5,7 +5,7 @@ permissions: on: pull_request: - types: [opened, reopened, synchronize] + types: [opened, reopened, synchronize, ready_for_review] jobs: tests: diff --git a/README.md b/README.md index bc8db5a..c4c2ad3 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ Changes must land on the branch corresponding to the earliest release that they `release/1.7.0` → `release/1.8.x` → `main` -For example, anything landing on `release/1.6.0` will eventually appear on `release/1.7.0`, then `release/1.8.x` and finally `main` as well. There is no need to file standalone PRs for each release line. (Change propagation currently requires manual work -- it is performed by project maintainers.) +For example, anything landing on `release/1.7.0` will eventually appear on `release/1.8.x` and finally `main` as well. There is no need to file standalone PRs for each release line. (Merges into `main` are performed by the [automerger](.github/workflows/automerge_to_main.yml); propagation between release branches is performed by project maintainers.) ### Code of Conduct