[swift-users] [Swift3][Linux][URLSession]: Core dump when trying to make simple HTTP request

Steven Harms sgharms at stevengharms.com
Sat Oct 29 11:02:52 CDT 2016


All,

Thanks for all your help thus far. I'll have some time this weekend to try
things out but wanted to let you know that I'm only using Linux-native
tools: vim, swift binary from the command line.

To this end: meaningful error messages, friendly API documentation,
comparability to Python / Node / Ruby for interacting with the web etc. are
all the things I'm investigating. Apparently a task that's come up is
friendliness around coding around the asynchrony model ;)

Thanks for the suggestions, I'll work through them soon and report back.

On Thu, Oct 27, 2016 at 6:32 PM, Dave Abrahams via swift-users <
swift-users at swift.org> wrote:

>
> Pasting a reply on behalf of Matt Wright (cc'd):
>
> > On Oct 27, 2016, at 1:57 PM, Dave Abrahams <dabrahams at apple.com> wrote:
> >
> >
> > From: Steven Harms via swift-users <swift-users at swift.org>
> > Subject: [Swift3][Linux][URLSession]: Core dump when trying to make
> simple HTTP request
> ...<schnipp 40>...
> > Date: October 27, 2016 at 5:01:58 AM PDT
> > To: swift-users at swift.org
> > Reply-To: Steven Harms <sgharms at stevengharms.com>
> >
> >
> > Hello Swift,
> >
> > I've been trying out Swift as a general utility language on the server
> (replacing Ruby or Node). I'm trying to write a simple image retriever via
> HTTP as a learning project.
> >
> > Inspired by this [gist][], I've written the following:
> >
> > let queue = DispatchQueue.global(qos: .background)
> >
> > let sessionConfiguration = URLSessionConfiguration.default
> > let session = URLSession(configuration: sessionConfiguration)
> >
> > print("staring sync")
> > queue.async(execute: {
> >     print(">>> [\(queue.label)]: At the queue start")
> >     print("x")
> >     let task = session.dataTask(with: URL(string: "http://google.com")!,
> completionHandler: {
> >         (data, response, error) in
> >         print("Task ran!")
> >     })
> >     print("y")
> >     task.resume()
> >     print("z")
> > })
> > print("ending sync")
> >
> > With output:
> >
> > staring sync
> > ending sync
> >
> > or...
> >
> > staring sync
> > ending sync
> > >>> [com.apple.root.background-qos]: At the queue start
> > x
> > y
> > y
> > z
> >
> > Whoa.
> >
> > At this point I'm going to have to confess that I need some help
> clarifying my model.
> >
> > 1. How did point "y" get fired twice? Or how did it happen not at all?
>
> The double-fire is something I can’t immediately come up with an
> explanation. It might be specific to the Linux dispatch
> implementation, which I’m not well versed on but it’s very strange
> that it would print twice. Not firing at all is likely because his
> snippet of code exited before the work was run on another thread. The
> “main” thread here either has to wait for the session work to finish
> explicitly, or call dispatch_main() to cede control of the main thread
> to the system (and subsequently exit out of the tool somewhere else).
>
> > 2. How did my callback for dataTask *never* fire? Even if the connection
> task *failed* the handler
> ought have fired, no?
>
> Likely similar to (1), though if URLSession uses runloops (Linux?)
> then it could also be because the runloop was never run anywhere. On
> Darwin I’d suspect the runloop for URLSession wasn’t running, I don’t
> know how it works on Linux.
>
> > 3. I didn't want to bring this whole queue business into the
> > picture, but it appears that without it the program exits before the
> > handler has a chance to fire.
>
> Probably similar to the issues in (1) and (2). If the snippet exits after
> running then a lot of this
> asynchronously scheduled work will never happen.
>
> > 4. Changing to a queue.sync from queue.async consistently produces the
> output I expect, but does
> not fire my completionHandler still.
>
> This forces the queue.sync to run in-line (on the current thread) but
> the snippet will then likely still exit before anything happens to
> cause the URLSession to finish.
>
> --
> -Dave
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161029/aff9a863/attachment.html>


More information about the swift-users mailing list