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

URLSessionTask: implement InputStream #1629

Merged

Conversation

albertaleksieiev
Copy link
Contributor

@albertaleksieiev albertaleksieiev commented Jul 12, 2018

Allows using InputStream in URLRequest as httpBodyStream. Also, implement seekInputStream in NativeProtocol.

Also added test test_dataTaskWithHttpInputStream for URLSession.

@alblue
Copy link
Contributor

alblue commented Jul 30, 2018

@swift-ci please test

1 similar comment
@gwynne
Copy link
Contributor

gwynne commented Sep 19, 2018

@swift-ci please test

@albertaleksieiev
Copy link
Contributor Author

@swift-ci please test

@spevans
Copy link
Contributor

spevans commented Sep 27, 2018

@swift-ci please test

1 similar comment
@spevans
Copy link
Contributor

spevans commented Sep 27, 2018

@swift-ci please test

@albertaleksieiev
Copy link
Contributor Author

@spevans @Cellane can you please check my changes, thanks!

@Cellane
Copy link

Cellane commented Oct 4, 2018

@albertaleksieiev For me personally it looks great, and compiling a custom version of Swift with your change caused our Vapor app to finally stop randomly crashing in production, so huge thanks for that! But I have no power here 🤣

Copy link

@Cellane Cellane left a comment

Choose a reason for hiding this comment

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

LGTM, solved all my issues.

@albertaleksieiev
Copy link
Contributor Author

@Cellane I am happy to hear it 🤗

@albertaleksieiev
Copy link
Contributor Author

@Cellane what type of the problem do you have before? It may be a memory problem because without InputStream you need to load an entire object in memory it may cause OOM.

@millenomi
Copy link
Contributor

@ianpartridge do you think this is ready to go?

@Cellane
Copy link

Cellane commented Oct 4, 2018

@albertaleksieiev I’m not quite sure, we just saw random crashes and restarts in the server log, but we failed to investigate if any particular request handler was causing it. Personally, my suspicion was on request handlers that accept images and upload them to S3, but I don’t have anything to back that claim up.

Copy link
Contributor

@ianpartridge ianpartridge left a comment

Choose a reason for hiding this comment

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

Thanks for doing this work! I've put a few thoughts in.

@albertaleksieiev
Copy link
Contributor Author

@ianpartridge thanks for your review, I will fix it soon!

@robipresotto
Copy link

Could we merge it? I guess a lot of people is waiting for that.

@albertaleksieiev
Copy link
Contributor Author

@robipresotto I'll commit latest changes in a few days(most likely at weekends).

@spevans
Copy link
Contributor

spevans commented Oct 20, 2018

@albertaleksieiev This PR now has conflicts, I would suggest rebasing it on origin/master then the tests can be run.

…nto url-session-task/input-stream

# Conflicts:
#	TestFoundation/HTTPServer.swift
@albertaleksieiev
Copy link
Contributor Author

@spevans fixed! Thanks for the quick reply :)

@spevans
Copy link
Contributor

spevans commented Oct 20, 2018

@swift-ci test

@albertaleksieiev
Copy link
Contributor Author

albertaleksieiev commented Jan 17, 2019

We can resolve it. Current libcurl only use SEEK_SET https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/URLSession/libcurl/EasyHandle.swift#L602 so we need to reset InputStream to position 0 and move forward to position. But the main problem is InputStream can't be seeked to position n if current position > n, InputStream can be moved only forward not backward. Resolution: Let’s move only forward and if curl asks us to move backward we will return CFURLSessionSeekCantSeek.

And if URLSession:task:needNewBodyStream delegate is setted we can rewind it back.

@albertaleksieiev
Copy link
Contributor Author

Oh, I little bit was wrong here, we just need to seek forward InputStream 🙂 We assume that URLSession:task:needNewBodyStream provides new InputStream at position 0.

@albertaleksieiev
Copy link
Contributor Author

@spevans can you please trigger a CI, thanks 🤗

@ianpartridge
Copy link
Contributor

@swift-ci please test

@albertaleksieiev
Copy link
Contributor Author

/home/buildnode/jenkins/workspace/swift-corelibs-foundation-PR-Linux/swift-corelibs-foundation/TestFoundation/TestStream.swift:181:42: 
error: '_Error' is inaccessible due to 'internal' protection level
        } catch let error as InputStream._Error {
                                         ^
<unknown>:0: note: type declared here
/home/buildnode/jenkins/workspace/swift-corelibs-foundation-PR-Linux/swift-corelibs-foundation/TestFoundation/TestStream.swift:157:24: 
error: 'seek' is inaccessible due to 'internal' protection level
            try stream.seek(to: pos)
                       ^
<unknown>:0: note: 'seek' declared here

It seems that NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT returns false for Linux. I do "@testable import Foundation` as:

#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
    #if (os(Linux) || os(Android))
        @testable import Foundation
    #else
        @testable import SwiftFoundation
    #endif
#endif

I took it from here, am I doing something wrong? 😅

@albertaleksieiev
Copy link
Contributor Author

I just get rid of NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT, can someone trigger a CI? Thanks!

@spevans
Copy link
Contributor

spevans commented Jan 24, 2019

@swift-ci test

@ddunbar
Copy link
Contributor

ddunbar commented Jan 24, 2019

How is this PR related to #1667 ?

@albertaleksieiev
Copy link
Contributor Author

@ddunbar It's not related to your mentioned PR at all. Here we need to seek InputStream, so I added a function seek to InputStream internal extension, and write a test for this seek function. So now our provided seekInputStream function to curl using our internal seek function.

@albertaleksieiev
Copy link
Contributor Author

Probably I can leave seek implementation inside seekInputStream function, but it will be harder to test.

@robipresotto
Copy link

Hey Guys - We are experiencing the issue below in a swift server-side application.

Fatal error: seekInputStream(to:) is not yet implemented: file Foundation/URLSession/NativeProtocol.swift, line 264

It looks like the PR will fix the issue and wondering if someone could help me to build a swift docker image with this PR merged so we can test it properly before merging it on master.

@nerzh
Copy link

nerzh commented Feb 7, 2019

@robipresotto just reboot your app for example every 5 minutes, I am not joking, because otherwise I have a non working simple app with http protocol, I do it with all my linux projects based on swift and URLSession. Really cool, I am right ? =)

@albertaleksieiev
Copy link
Contributor Author

Can someone please trigger a CI? Thanks 🙂

@ianpartridge
Copy link
Contributor

@swift-ci test

@robipresotto
Copy link

Ship it 😅

@albertaleksieiev
Copy link
Contributor Author

@ianpartridge What to do next? 🙃Or may someone else know?

@robipresotto
Copy link

Could we merge it?

@parkera
Copy link
Contributor

parkera commented Feb 20, 2019

@swift-ci test and merge

@swift-ci swift-ci merged commit bb14813 into swiftlang:master Feb 20, 2019
@albertaleksieiev
Copy link
Contributor Author

Thanks to everyone 🙌

@ianpartridge
Copy link
Contributor

Can you cherry-pick this to swift-5.0-branch?

@albertaleksieiev
Copy link
Contributor Author

@ianpartridge for sure 👍

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

Successfully merging this pull request may close these issues.