diff --git a/Sources/System/MachPort.swift b/Sources/System/MachPort.swift index a67a01a..4ca3572 100644 --- a/Sources/System/MachPort.swift +++ b/Sources/System/MachPort.swift @@ -81,17 +81,17 @@ public enum Mach { } deinit { - if _name == 0xFFFFFFFF /* MACH_PORT_DEAD */ { - precondition(RightType.self != ReceiveRight.self, "Receive rights cannot be dead names") - _machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_DEAD_NAME, -1)) + if RightType.self == ReceiveRight.self { + precondition(_name != 0xffffffff, "Receive rights cannot be dead names") + _machPrecondition( + mach_port_destruct(mach_task_self_, _name, 0, _context) + ) } else { - if RightType.self == ReceiveRight.self { - _machPrecondition(mach_port_destruct(mach_task_self_, _name, 0, _context)) - } else if RightType.self == SendRight.self { - _machPrecondition(mach_port_deallocate(mach_task_self_, _name)) - } else if RightType.self == SendOnceRight.self { - _machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_SEND_ONCE, -1)) - } + assert( + RightType.self == SendRight.self || + RightType.self == SendOnceRight.self + ) + _machPrecondition(mach_port_deallocate(mach_task_self_, _name)) } } } diff --git a/Tests/SystemTests/MachPortTests.swift b/Tests/SystemTests/MachPortTests.swift index bf1e118..3c64ce8 100644 --- a/Tests/SystemTests/MachPortTests.swift +++ b/Tests/SystemTests/MachPortTests.swift @@ -210,6 +210,20 @@ final class MachPortTests: XCTestCase { XCTAssertEqual(context, $1) } } + + func testDeinitDeadSendRight() throws { + let send = Mach.Port(name: 0xffffffff) + send.withBorrowedName { + XCTAssertEqual($0, .max) + } + _ = consume send + + let send1 = Mach.Port(name: 0xffffffff) + send1.withBorrowedName { + XCTAssertEqual($0, .max) + } + _ = consume send1 + } } #endif