Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for WASILibc #2671

Merged
merged 26 commits into from
Sep 12, 2024
Merged

Add support for WASILibc #2671

merged 26 commits into from
Sep 12, 2024

Conversation

MaxDesiatov
Copy link
Member

@MaxDesiatov MaxDesiatov commented Mar 4, 2024

Dispatch is not supported on WASI, and only Unix domain sockets are supported, which means we have to exclude those APIs on this platform.

There's work in progress to enable tests for this on CI, but nothing I can provide for this PR at the current moment.

Dispatch is not supported on WASI, and only Unix domain sockets are supported, which means we have to exclude those APIs on this platform.
Sources/NIOCore/FileHandle.swift Outdated Show resolved Hide resolved
Sources/NIOCore/BSDSocketAPI.swift Outdated Show resolved Hide resolved
Sources/NIOCore/Interfaces.swift Outdated Show resolved Hide resolved
Sources/NIOCore/SocketAddresses.swift Outdated Show resolved Hide resolved
@MaxDesiatov MaxDesiatov marked this pull request as draft March 4, 2024 16:18
@MaxDesiatov MaxDesiatov marked this pull request as ready for review March 8, 2024 15:33
Package.swift Outdated Show resolved Hide resolved
Package.swift Outdated Show resolved Hide resolved
@Lukasa
Copy link
Contributor

Lukasa commented Mar 8, 2024

@swift-server-bot add to allowlist

Package.swift Outdated Show resolved Hide resolved
Package.swift Show resolved Hide resolved
@@ -45,7 47,7 @@ public final class Lock {
public init() {
#if os(Windows)
InitializeSRWLock(self.mutex)
#else
#elseif !os(WASI)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can merge a PR that silently disables these primitives on WASI without being extremely confident that we'll never need them. Is there any way we can replace this with a feature-detection on threading?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swift main snapshots support #if _runtime(_multithreaded) checks, which I'm now using here.

@@ -199,12 204,14 @@ extension NIOBSDSocket.ProtocolFamily {
public static let inet6: NIOBSDSocket.ProtocolFamily =
NIOBSDSocket.ProtocolFamily(rawValue: PF_INET6)

#if !os(WASI)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My general preference here would be that the APIs here are present, they just end up failing when actually used in WASI. That would mean hardcoding the syscall value on WASI, or deliberately using a spoiler value. That note applies to the rest of this file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the benefit of that as compared to compile-time warnings for absent symbols? My worry is that someone ends up adopting these APIs when targeting WASI and then discovering that they fail at the most inconvenient moment.

If you insist on runtime errors, would you prefer a fatalError or a certain sentinel value? In the latter case, how do I pick such value or do we just change NIOBSDSocket.Option.rawValue to have CInt? type instead of CInt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One other argument for compile-time checks is consistency: Windows did it that way, so why WASI shouldn't follow that precedent?

Sources/NIOCore/EventLoop.swift Outdated Show resolved Hide resolved
@@ -54,6 57,7 @@ public final class NIOFileHandle: FileDescriptor {
assert(!self.isOpen, "leaked open NIOFileHandle(descriptor: \(self.descriptor)). Call `close()` to close or `takeDescriptorOwnership()` to take ownership and close by some other means.")
}

#if !os(WASI)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same note here: generally it's preferable to have APIs not simply be absent, but to misbehave when not present. In this case, duplicate will presumably become available, and it'd be nicer if this just threw instead. The only time that isn't my preference is when an API literally cannot be spelled, e.g. the return or argument types are missing.

Relatedly, it's best if we put the #if os at the lowest possible level. In this case, we should arrange for dup to throw or error out, and let the rest of the code path handle that appropriately, which minimises the OS customisation we have to do.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, what's the benefit of preferring obscured runtime errors when we can easily make these compile-time errors and make unavailability obvious to a library user?

@@ -445,6 456,7 @@ public enum SocketAddress: CustomStringConvertible, Sendable {
self = .v6(.init(address: ipv6Addr, host: ""))
}

#if !os(WASI)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make makeAddressResolvingHost just throw exception instead of guarding out the API itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure why we'd prefer runtime checks as opposed to compile-time checks, but this particular change seems easy, so I pushed it for now.

@@ -140,6 142,7 @@ public protocol SocketOptionProvider: _NIOPreconcurrencySendable {
//
// You are welcome to add more helper methods here, but each helper method you add must be tested.
extension SocketOptionProvider {
#if !os(WASI)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding a typealias like the following instead of guarding out the whole method definition?

#if canImport(WASILibc)
typealias NIOLinger = Never
#else
typealias NIOLinger = linger
#endif

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as before, unsure why we'd prefer runtime failures, but this one seemed trivial so I'd considered it addressed

@weissi
Copy link
Member

weissi commented Apr 9, 2024

@MaxDesiatov / @Lukasa This looks like a PR that has bit rotting potential because it needs to touch a lot of pieces, most of the issues seem pretty tractable to me so I'd love to see this go in.

w.r.t. disabling locks on WASI: Honestly I think this can go in as is: Today this is fine and if threading really happens this will blow up very obviously and can be fixed.

# Conflicts:
#	Package.swift
#	Sources/NIOConcurrencyHelpers/NIOLock.swift
#	Sources/NIOConcurrencyHelpers/atomics.swift
#	Sources/NIOConcurrencyHelpers/lock.swift
#	Sources/NIOCore/BSDSocketAPI.swift
#	Sources/NIOCore/ByteBuffer-aux.swift
#	Sources/NIOCore/ByteBuffer-core.swift
#	Sources/NIOCore/EventLoop.swift
#	Sources/NIOCore/EventLoopFuture.swift
#	Sources/NIOCore/FileHandle.swift
#	Sources/NIOCore/FileRegion.swift
#	Sources/NIOCore/GlobalSingletons.swift
#	Sources/NIOCore/IO.swift
#	Sources/NIOCore/Interfaces.swift
#	Sources/NIOCore/MulticastChannel.swift
#	Sources/NIOCore/SystemCallHelpers.swift
#	Sources/NIOCore/Utilities.swift
#	Sources/NIOEmbedded/AsyncTestingChannel.swift
#	Sources/NIOEmbedded/Embedded.swift
#	Sources/NIOPosix/GetaddrinfoResolver.swift
#	Sources/NIOPosix/MultiThreadedEventLoopGroup.swift
@MaxDesiatov
Copy link
Member Author

Currently blocked on swiftlang/swift#75618, which would make PF_INET and PF_INET6 available.

@MaxDesiatov MaxDesiatov marked this pull request as draft August 1, 2024 14:56
@weissi
Copy link
Member

weissi commented Aug 1, 2024

Currently blocked on swiftlang/swift#75618, which would make PF_INET and PF_INET6 available.

You can just hardcode the values for the time being, right? They are #defines and therefore ABI, so they can't change.

@MaxDesiatov
Copy link
Member Author

Currently blocked on swiftlang/swift#75618, which would make PF_INET and PF_INET6 available.

You can just hardcode the values for the time being, right? They are #defines and therefore ABI, so they can't change.

If that version bump is easy and we get it done quickly, I'd rather have that one merged and make this PR cleaner at the same time.

@MaxDesiatov MaxDesiatov marked this pull request as ready for review September 4, 2024 16:18
@MaxDesiatov
Copy link
Member Author

MaxDesiatov commented Sep 4, 2024

With changes I've pushed the build is now successful with latest development snapshots when building NIOCore and NIOHTTP1 targets for single-threaded WASI.

@MaxDesiatov
Copy link
Member Author

Pushed more changes so that it uses wasi_pthread when multi-threading is available with wasm32-unknown-wasip1-threads triples.

@MaxDesiatov
Copy link
Member Author

MaxDesiatov commented Sep 4, 2024

w.r.t. disabling locks on WASI: Honestly I think this can go in as is: Today this is fine and if threading really happens this will blow up very obviously and can be fixed.

@weissi In this PR we now conditionally enable locks when those are available in WASI.

@weissi
Copy link
Member

weissi commented Sep 9, 2024

CC @FranzBusch & @Lukasa

Copy link
Member

@FranzBusch FranzBusch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM. @Lukasa do you want to review as well?

@FranzBusch FranzBusch added the semver/patch No public API change. label Sep 11, 2024
Copy link
Member

@weissi weissi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @MaxDesiatov !

@Lukasa Lukasa merged commit 730713e into apple:main Sep 12, 2024
28 of 29 checks passed
ali-ahsan-ali pushed a commit to ali-ahsan-ali/swift-nio that referenced this pull request Sep 15, 2024
Dispatch is not supported on WASI, and only Unix domain sockets are
supported, which means we have to exclude those APIs on this platform.

There's work in progress to enable tests for this on CI, but nothing I
can provide for this PR at the current moment.

---------

Co-authored-by: Franz Busch <[email protected]>
cgrindel-self-hosted-renovate bot referenced this pull request in cgrindel/rules_swift_package_manager Sep 25, 2024
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [apple/swift-nio](https://redirect.github.com/apple/swift-nio) | minor
| `2.72.0` -> `2.73.0` |

---

### Release Notes

<details>
<summary>apple/swift-nio (apple/swift-nio)</summary>

###
[`v2.73.0`](https://redirect.github.com/apple/swift-nio/releases/tag/2.73.0)

[Compare
Source](https://redirect.github.com/apple/swift-nio/compare/2.72.0...2.73.0)

<!-- Release notes generated using configuration in .github/release.yml
at main -->

#### What's Changed

##### SemVer Minor

- Make `ByteBuffer`'s description more useful by
[@&#8203;supersonicbyte](https://redirect.github.com/supersonicbyte) in
[https://github.com/apple/swift-nio/pull/2864](https://redirect.github.com/apple/swift-nio/pull/2864)
- Expose `UDP_MAX_SEGMENTS` via System by
[@&#8203;rnro](https://redirect.github.com/rnro) in
[https://github.com/apple/swift-nio/pull/2891](https://redirect.github.com/apple/swift-nio/pull/2891)
- Add new `ChannelOption` to get the amount of buffered outbound data in
the Channel by
[@&#8203;johnnzhou](https://redirect.github.com/johnnzhou) in
[https://github.com/apple/swift-nio/pull/2849](https://redirect.github.com/apple/swift-nio/pull/2849)
- Add an `AcceptBackoffHandler` to the async server bootstraps by
[@&#8203;FranzBusch](https://redirect.github.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2782](https://redirect.github.com/apple/swift-nio/pull/2782)

##### SemVer Patch

- Adding a nicer description for `WebSocketFrame` by
[@&#8203;supersonicbyte](https://redirect.github.com/supersonicbyte) in
[https://github.com/apple/swift-nio/pull/2862](https://redirect.github.com/apple/swift-nio/pull/2862)
- Improving `description` and adding `debugDescription` to `NIOAny` by
[@&#8203;supersonicbyte](https://redirect.github.com/supersonicbyte) in
[https://github.com/apple/swift-nio/pull/2866](https://redirect.github.com/apple/swift-nio/pull/2866)
- Make FileChunk sendable by
[@&#8203;ali-ahsan-ali](https://redirect.github.com/ali-ahsan-ali) in
[https://github.com/apple/swift-nio/pull/2871](https://redirect.github.com/apple/swift-nio/pull/2871)
- Make `ByteBuffer.debugDescription` suitable for structural display by
[@&#8203;dnadoba](https://redirect.github.com/dnadoba) in
[https://github.com/apple/swift-nio/pull/2495](https://redirect.github.com/apple/swift-nio/pull/2495)
- Add support for WASILibc by
[@&#8203;MaxDesiatov](https://redirect.github.com/MaxDesiatov) in
[https://github.com/apple/swift-nio/pull/2671](https://redirect.github.com/apple/swift-nio/pull/2671)
- `NIOSingleStepByteToMessageDecoder` reentrancy safety by
[@&#8203;rnro](https://redirect.github.com/rnro) in
[https://github.com/apple/swift-nio/pull/2881](https://redirect.github.com/apple/swift-nio/pull/2881)
- Adopt `NIOThrowingAsyncSequenceProducer` by
[@&#8203;rnro](https://redirect.github.com/rnro) in
[https://github.com/apple/swift-nio/pull/2879](https://redirect.github.com/apple/swift-nio/pull/2879)
- Clamp buffer to maximum upon large write operation by
[@&#8203;ali-ahsan-ali](https://redirect.github.com/ali-ahsan-ali) in
[https://github.com/apple/swift-nio/pull/2745](https://redirect.github.com/apple/swift-nio/pull/2745)
- Revert "Adopt `NIOThrowingAsyncSequenceProducer`
([#&#8203;2879](https://redirect.github.com/apple/swift-nio/issues/2879))"
by [@&#8203;rnro](https://redirect.github.com/rnro) in
[https://github.com/apple/swift-nio/pull/2892](https://redirect.github.com/apple/swift-nio/pull/2892)
- Add concrete description for `EmbeddedEventLoop` by
[@&#8203;aryan-25](https://redirect.github.com/aryan-25) in
[https://github.com/apple/swift-nio/pull/2890](https://redirect.github.com/apple/swift-nio/pull/2890)
- Conditionally include linux/udp.h by
[@&#8203;rnro](https://redirect.github.com/rnro) in
[https://github.com/apple/swift-nio/pull/2894](https://redirect.github.com/apple/swift-nio/pull/2894)
- Work around a type checking error when using the Static Linux SDK by
[@&#8203;euanh](https://redirect.github.com/euanh) in
[https://github.com/apple/swift-nio/pull/2898](https://redirect.github.com/apple/swift-nio/pull/2898)

##### Other Changes

- \[CI] Run tests on push to main by
[@&#8203;FranzBusch](https://redirect.github.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2868](https://redirect.github.com/apple/swift-nio/pull/2868)
- \[CI] License header support `.in` and `.cmake` files by
[@&#8203;FranzBusch](https://redirect.github.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2870](https://redirect.github.com/apple/swift-nio/pull/2870)
- Include nanoseconds in assertion of timestamp for NIOFileSystem tests
by [@&#8203;gjcairo](https://redirect.github.com/gjcairo) in
[https://github.com/apple/swift-nio/pull/2869](https://redirect.github.com/apple/swift-nio/pull/2869)
- Correct the link of sswg-security at SECURITY.md by
[@&#8203;lamtrinhdev](https://redirect.github.com/lamtrinhdev) in
[https://github.com/apple/swift-nio/pull/2872](https://redirect.github.com/apple/swift-nio/pull/2872)
- Speculative fix for flakey AsyncTestingEventLoop test by
[@&#8203;simonjbeaumont](https://redirect.github.com/simonjbeaumont) in
[https://github.com/apple/swift-nio/pull/2873](https://redirect.github.com/apple/swift-nio/pull/2873)
- ci: Install shellcheck if not present in CI runner by
[@&#8203;simonjbeaumont](https://redirect.github.com/simonjbeaumont) in
[https://github.com/apple/swift-nio/pull/2882](https://redirect.github.com/apple/swift-nio/pull/2882)
- ci: Use ${GITHUB_BASE_REF} as treeish for checking API break by
[@&#8203;simonjbeaumont](https://redirect.github.com/simonjbeaumont) in
[https://github.com/apple/swift-nio/pull/2883](https://redirect.github.com/apple/swift-nio/pull/2883)
- ci: Refer to nested reusable workflows using remote variant by
[@&#8203;simonjbeaumont](https://redirect.github.com/simonjbeaumont) in
[https://github.com/apple/swift-nio/pull/2884](https://redirect.github.com/apple/swift-nio/pull/2884)
- \[CI] Fix pull request label workflow by
[@&#8203;FranzBusch](https://redirect.github.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2885](https://redirect.github.com/apple/swift-nio/pull/2885)

#### New Contributors

- [@&#8203;ali-ahsan-ali](https://redirect.github.com/ali-ahsan-ali)
made their first contribution in
[https://github.com/apple/swift-nio/pull/2871](https://redirect.github.com/apple/swift-nio/pull/2871)
- [@&#8203;aryan-25](https://redirect.github.com/aryan-25) made their
first contribution in
[https://github.com/apple/swift-nio/pull/2890](https://redirect.github.com/apple/swift-nio/pull/2890)
- [@&#8203;johnnzhou](https://redirect.github.com/johnnzhou) made their
first contribution in
[https://github.com/apple/swift-nio/pull/2849](https://redirect.github.com/apple/swift-nio/pull/2849)
- [@&#8203;euanh](https://redirect.github.com/euanh) made their first
contribution in
[https://github.com/apple/swift-nio/pull/2898](https://redirect.github.com/apple/swift-nio/pull/2898)

**Full Changelog**:
apple/swift-nio@2.72.0...2.73.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config
help](https://redirect.github.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://redirect.github.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC45NC4xIiwidXBkYXRlZEluVmVyIjoiMzguOTQuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: cgrindel-self-hosted-renovate[bot] <139595543 cgrindel-self-hosted-renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants