apple / swift-nio

Event-driven network application framework for high performance protocol servers & clients, non-blocking.
https://swiftpackageindex.com/apple/swift-nio/documentation
Apache License 2.0
7.85k stars 633 forks source link

NIOSendableBox: allow off-loop initialisation iff Value is Sendable #2753

Closed weissi closed 1 week ago

weissi commented 1 week ago

Motivation:

NIOLoopBound and NIOLoopBoundBox are important tools for making mutable types Sendable. The power of NIOLoopBoundBox is that it allows correct mutation, whilst remaining Sendable without locks and without having to use @unchecked if the mutable class makes sure to have all accesses (reading & writing) run on one particular EventLoop. This is safe as EventLoops guarantee sequentially consistent memory order across executions of different work items/events. Typically EventLoops achieve this by just being thread-bound.

These types are well used already but there's a small and common pattern which is safe but unsupported as of yet:

Initialise a NIOLoopBoundBox with a Sendable value off the loop. All further accesses (reads & writes) however happen on the loop. That's safe because of Swift's Definitive Initialisation (DI) but NIOLoopBoundBox didn't support this pattern (apart from makeEmptyBox which always initialises with nil).

Modifications:

Result:

weissi commented 1 week ago
19:46:31 Test Case 'NIOConcurrencyHelpersTests.testLoadAndStoreHammeBuild timed out (after 20 minutes). Marking the build as failed.
weissi commented 1 week ago

@swift-nio-bot test failed

weissi commented 1 week ago

@swift-nio-bot test this please