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

URLSession not usable when compiled with Static Linux SDK #5092

Open
ffried opened this issue Oct 2, 2024 · 5 comments
Open

URLSession not usable when compiled with Static Linux SDK #5092

ffried opened this issue Oct 2, 2024 · 5 comments
Labels

Comments

@ffried
Copy link
Contributor

ffried commented Oct 2, 2024

Using the following Package.swift:

// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let swiftSettings: Array<SwiftSetting> = [
    .swiftLanguageMode(.v6),
    .enableUpcomingFeature("ExistentialAny"),
    .enableUpcomingFeature("InternalImportsByDefault"),
]

let package = Package(
    name: "StaticNetworking",
    platforms: [.macOS(.v15)],
    targets: [
        .executableTarget(
            name: "StaticNetworking",
            swiftSettings: swiftSettings,
            // Workaround for https://github.com/swiftlang/swift-corelibs-foundation/issues/5089
            linkerSettings: [
                .linkedLibrary("crypto", .when(platforms: [.linux])),
                .linkedLibrary("icudata", .when(platforms: [.linux])),
                .linkedLibrary("icuuc", .when(platforms: [.linux])),
                .linkedLibrary("ssl", .when(platforms: [.linux])),
                .linkedLibrary("z", .when(platforms: [.linux])),
            ]),
    ]
)

With the following Sources/main.swift:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

struct Post: Sendable, Codable {
    let id: Int
    let userId: Int
    let title: String
    let body: String
}

let request = URLRequest(url: URL(string: "https://jsonplaceholder.typicode.com/posts")!)
print("Fetching data...")
let (data, response) = try await URLSession.shared.data(for: request)
print("Response: \(response)")
let posts = try JSONDecoder().decode(Array<Post>.self, from: data)
print("Decoded \(posts.count) post(s)...")

Which is then built using the Swift Static Linux SDK (swift build -c release --swift-sdk "x86_64-swift-linux-musl"), leads to the following error when run (on e.g. an Ubuntu 24.04 machine):

Fetching data...
Swift/ErrorType.swift:253: Fatal error: Error raised at top level: Error Domain=NSURLErrorDomain Code=-1 "(null)"
Current stack trace:
0    <unknown>                          0x0000000000cb54a1
1    <unknown>                          0x00000000008c09d7
2    <unknown>                          0x00000000008c07e3
3    <unknown>                          0x00000000008c0c39
4    <unknown>                          0x0000000000a7fa46
5    <unknown>                          0x0000000000adc914
6    <unknown>                          0x00000000006e600e
7    <unknown>                          0x0000000000cb9a33
8    <unknown>                          0x0000000000cba655
9    <unknown>                          0x0000000000db400b
10   <unknown>                          0x0000000000db4928
11   <unknown>                          0x0000000000dba506
Illegal instruction

Note the NSURLErrorDomain Code=-1 which is NSURLErrorUnknown.
It doesn't matter which url is used, the result is always the same.

Tested using Swift 6.0.1 and Swift Static Linux SDK 0.0.1.
It's also irrelevant whether the executable is built on macOS or Linux.

@MaxDesiatov
Copy link
Contributor

cc @al45tair

@TimBudarick
Copy link

TimBudarick commented Nov 8, 2024

We have the same problem. We have also tried many variants.

  1. We tried to add another libcurl library to the linker. Because I have read that the URLSession under linux works with libcurl.

  2. We have tried different virtual machines. We have tried ARM64 Debain and Ubuntu Server in different versions. Under Ubuntu 24.04.1 and 24.10. Under Debian we only tested the latest version. We also tried to install all CURL versions on the system. However, this should not be relevant, as all required libraries should normally be linked directly. Please correct me if I am wrong.

  3. We have tried various Docker containers. We have tried the containers: ubuntu:latest, debian:latest, swift:latest, swift:slim.

  4. We checked the corresponding request headers. In the linked issue, the user “0xfeedface1993” reported that this problem could usually be solved by setting the “accept-encoding” header. Issue: [SR-14677] FoundationNetworking web request error on Debian/TwisterOS #3959

Unfortunately, none of this worked. I also tried several Linux Static SDK versions, unfortunately always the same behavior.

The really strange thing is also that when we compile the source code with Docker. This means that we create a builder container in which the source code is compiled and then transferred to a productive container. We are referring here to the Dockerfile from Vapor.
We get exactly the same error even in the Docker container.

@MaxDesiatov
Copy link
Contributor

MaxDesiatov commented Nov 8, 2024

For comparison, do you get the same results with https://github.com/swift-server/async-http-client if you have a chance to try that out?

@ffried
Copy link
Contributor Author

ffried commented Nov 8, 2024

@MaxDesiatov Nope, async-http-client works. I've actually switched the project that I found this bug initially over to async-http-client and now it works.
However, this also multiplied the compile time by a lot since the whole SwiftNIO stack is now drawn into it.

@al45tair
Copy link
Contributor

al45tair commented Nov 8, 2024

FWIW, I'd started trying to look at this, but got stuck because of another problem; hopefully that issue will have worked its way through now and I'll be able to get back to looking at this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants