FoundationNetworking Changes?

I'm revisiting a project that worked in January and is now failing a basic test, wondering if anyone can point me to some obvious updates on Swift for Linux, or perhaps a Linux update that might be causing the trouble.

The code is simply an HTTP authentication request (Reddit), which still passes on Mac, but started failing on Linux sometime in the past few months:

        #if canImport(FoundationNetworking)
        let data: Data = await withCheckedContinuation { continuation in
                URLSession.shared.dataTask(with: request) { data, _, _ in
                    guard let data = data else {
                        fatalError() // Now breaks here
                    }
                    continuation.resume(returning: data)
                }.resume()
            }
        #else
        let (data, _) = try await session.data(for: request)
        #endif

I'm new to this kind of networking code as well as Swift on Linux, so any pointers are aprpeciated. Where would you look first for debugging this? Swift or OS?

Simplest thing to check is to stop ignoring the other closure parameters and see what response and error you may have gotten.

1 Like

Thanks, that would go something like this?

 #if canImport(FoundationNetworking)
        let data: Data = await withCheckedContinuation { continuation in
                URLSession.shared.dataTask(with: request) { data, response, error in
                    guard let data = data else {
                        print(response)
                        print(error)
                        fatalError()
                    }
                    continuation.resume(returning: data)
                }.resume()
            }
        #else
        let (data, _) = try await session.data(for: request)
        #endif

At the risk of giving TMI, I just get this stack trace:

Summary

Test Case 'RedditSessionEndpointTest.testCommentSearch' started at 2024-04-12 19:30:12.382
SwiftNLP/Session + Authentication.swift:75: Fatal error
*** Signal 4: Backtracing from 0x7f74dee56da6...
done ***
*** Program crashed: Illegal instruction at 0x00007f74dee56da6 ***
Thread 0 "SwiftNLPPackage":
0 0x00007f74ddda9cce in libc.so.6
Thread 1:
0 0x00007f74ddd22117 in libc.so.6
Thread 2:
0 0x00007f74dddb6e2e in libc.so.6
Thread 3:
0 0x00007f74ddd22117 in libc.so.6
Thread 4:
0 0x00007f74ddd22117 in libc.so.6
Thread 5:
0 0x00007f74ddd22117 in libc.so.6
Thread 6 crashed:
0 0x00007f74dee56da6 assertionFailure(:_:file:line:flags:) + 502 in libswiftCore.so
1 [ra] 0x0000562542fcf22a closure #1 in closure #2 in Session.authenticate() + 361 in SwiftNLPPackageTests.xctest at /home/gitlab-runner/builds/uauRNHyjd/0/jrwallace/swiftnlp/Sources/SwiftNLP/1. Data Collection/Reddit API/Authentication/Session + Authentication.swift:75:25
2 [ra] 0x00007f74de109b4c closure #3 in _ProtocolClient.urlProtocol(task:didFailWithError:) + 75 in libFoundationNetworking.so
Registers:
rax 0x00007f74df0fbbb0 55 48 89 e5 48 85 ff 7e 26 48 b9 00 00 00 00 fe UHยทรฅHยทรฟ~&Hยนยทยทยทยทรพ
rdx 0x000056254303f355 46 61 74 61 6c 20 65 72 72 6f 72 00 6f 66 66 73 Fatal errorยทoffs
rcx 0x00007f74df0fbbb0 55 48 89 e5 48 85 ff 7e 26 48 b9 00 00 00 00 fe UHยทรฅHยทรฟ~&Hยนยทยทยทยทรพ
rbx 0x0000000562543412 23124522002
rsi 0x8000000000000000 9223372036854775808
rdi 0x00007f749c0008e0 01 00 02 00 01 00 02 00 01 00 02 00 00 00 00 00 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท
rbp 0x00007f74cbffe7f0 60 e8 ff cb 74 7f 00 00 2a f2 fc 42 25 56 00 00 `รจรฟร‹tยทยทยท*รฒรผB%Vยทยท
rsp 0x00007f74cbffe750 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท
r8 0x0000562543412ac0 12 34 54 62 05 00 00 00 40 6c 11 72 1a d5 7e 7a ยท4Tbยทยทยทยท@lยทrยทร•~z
r9 0x00007f74cbffe490 b0 1a 24 df 74 7f 00 00 70 d8 26 df 74 7f 00 00 ยฐยท$รŸtยทยทยทpร˜&รŸtยทยทยท
r10 0x0000000000000000 0
r11 0x7a7ed51a72116c40 8826726629252361280
r12 0x0000000000000000 0
r13 0x0000000000000000 0
r14 0xe000000000000000 16140901064495857664
r15 0x000000000000000b 11
rip 0x00007f74dee56da6 0f 0b 48 83 ec 08 48 8d 05 3d ec 41 00 48 8d 3d ยทยทHยทรฌยทHยทยท=รฌAยทHยท=
rflags 0x0000000000010286 SF PF
cs 0x0033 fs 0x0000 gs 0x0000
Images (46 omitted):
0x0000562542ee4000โ€“0x00005625430335b8 SwiftNLPPackageTests.xctest /home/gitlab-runner/builds/uauRNHyjd/0/jrwallace/swiftnlp/.build/x86_64-unknown-linux-gnu/release/SwiftNLPPackageTests.xctest
0x00007f74ddc91000โ€“0x00007f74dde4d341 c289da5071a3399de893d2af81d6a30c62646e1e libc.so.6 /usr/lib/x86_64-linux-gnu/libc.so.6
0x00007f74de013000โ€“0x00007f74de145580 libFoundationNetworking.so /home/gitlab-runner/.local/share/swiftly/toolchains/5.9.2/usr/lib/swift/linux/libFoundationNetworking.so
0x00007f74decf3000โ€“0x00007f74df23a048 libswiftCore.so /home/gitlab-runner/.local/share/swiftly/toolchains/5.9.2/usr/lib/swift/linux/libswifterror: Exited with signal code 4
Core.so
Backtrace took 0.49s

Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit status 1

And I'm missing any response/error outputs ... I'm doing something bone-headed, right?

I would suggest you stop crashing and allow the code to exit cleanly so you can see the print output and resume the continuation in the error case.

1 Like

Jon suggests you should call continuation.resume(throwing: error) in case of error. To do that you'd first need to change withCheckedContinuation to withCheckedThrowingContinuation.


Note that the data parameter (even when non nil) could be not what you think, e.g.: "<page not found>" or something to that account. It's worth checking response.code:

let code = (response as! HTTPURLResponse)!.statusCode
let ok = code >= 200 && code < 300

if ok - then proceed with the data, otherwise data could be bogus (even if non nil). In that case you've got no error โ†’ create an error of your own (e.g. NSError(domain: "HTTPURLResponseError", code: 1234)) and resume the continuation with that error.

2 Likes