Workaround for networking crash on Windows?

Hi!

After the recent success of The Browser Company to bring a complex application written in Swift to Windows, I finally managed to convince my coworkers to give Swift on Windows a try. I've waited for years for the opportunity to prove that Swift is a viable solution, but unfortunately I might just prove them otherwise.

We tried rewriting a small app from Rust to Swift, but unfortunately for us it's an app that does a lot of networking. The problems started pretty fast, because to my surprise, URLSession is very different on non Apple platforms, it's even missing the most basic function: func data(for request: URLRequest, delegate: (URLSessionTaskDelegate)? = nil) async throws -> (Data, URLResponse).

That's not the biggest problem, as we can implement it ourselves. The main problem is that no matter what we do, the app crashes. It's probably related to this (URLSessionTasks on Windows can cause crashes), but even with the swift-backtrace, we're not getting any useful backtrace.

We tried using async-http-client but NIO doesn't build on Windows.

We tried using cURL as a .systemLibrary, and that appears to be working, but we can't require every user to have libcurl installed.

The last option I can think of is building libcurl ourselves as a static library but I think I read on the forum that .binaryTarget might not work on Windows and I'm not sure I have the energy left to learn how to build libcurl and then link it.

Has anyone figured out a way to do this? Or is there another alternative to URLSession/async-http-client?

2 Likes

I wonder if (and hope) this merged PR fixes most of the problems? Cancel `DispatchSource` before closing socket (#4791) by lxbndr · Pull Request #4859 · apple/swift-corelibs-foundation · GitHub

By the way, my interim solution was to write a Swift wrapper around WinHTTP and hope I did everything correctly. At minimum seems to not crash!