Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sources/Containerization/VZVirtualMachine+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ extension VZVirtioSocketConnection {
if fd == -1 {
throw POSIXError.fromErrno()
}
self.close()
return FileHandle(fileDescriptor: fd, closeOnDealloc: false)
let handle = FileHandle(fileDescriptor: fd, closeOnDealloc: false)
retainConnectionOwner(self, for: handle)
return handle
}
}

Expand Down
19 changes: 18 additions & 1 deletion Sources/Containerization/VsockListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Foundation
import Synchronization

#if os(macOS)
import ObjectiveC
import Virtualization
#endif

Expand Down Expand Up @@ -76,6 +77,22 @@ public final class VsockListener: NSObject, Sendable, AsyncSequence {

#if os(macOS)

private final class ConnectionOwnerAssociationKey: @unchecked Sendable {}

private let connectionOwnerAssociationKey = ConnectionOwnerAssociationKey()

/// Keeps an owner of a duplicated descriptor alive for as long as its file
/// handle can use that descriptor. Virtualization owns the original VSOCK
/// descriptor and requires its connection object to stay alive after accept.
func retainConnectionOwner(_ owner: AnyObject, for handle: FileHandle) {
objc_setAssociatedObject(
handle,
Unmanaged.passUnretained(connectionOwnerAssociationKey).toOpaque(),
owner,
.OBJC_ASSOCIATION_RETAIN_NONATOMIC
)
}

extension VsockListener: VZVirtioSocketListenerDelegate {
public func listener(
_: VZVirtioSocketListener, shouldAcceptNewConnection conn: VZVirtioSocketConnection,
Expand All @@ -85,9 +102,9 @@ extension VsockListener: VZVirtioSocketListenerDelegate {
guard fd != -1 else {
return false
}
conn.close()

let fh = FileHandle(fileDescriptor: fd, closeOnDealloc: false)
retainConnectionOwner(conn, for: fh)
let result = cont.yield(fh)
if case .terminated = result {
try? fh.close()
Expand Down
43 changes: 43 additions & 0 deletions Tests/ContainerizationTests/VsockListenerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===----------------------------------------------------------------------===//
// Copyright © 2025-2026 Apple Inc. and the Containerization project authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//

#if os(macOS)
import Foundation
import Testing

@testable import Containerization

struct VsockListenerTests {
@Test func connectionOwnerOutlivesTheDescriptorHandoff() throws {
var handle: FileHandle? = FileHandle(forReadingAtPath: "/dev/null")
try #require(handle != nil)

var owner: ConnectionOwner? = ConnectionOwner()
weak let weakOwner = owner
retainConnectionOwner(owner!, for: handle!)
owner = nil

#expect(weakOwner != nil)

try handle?.close()
handle = nil

#expect(weakOwner == nil)
}
}

private final class ConnectionOwner {}
#endif