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

Crash during upload #2930

Closed
varungakhar opened this issue Jan 2, 2023 · 3 comments · Fixed by #2939 or #2998
Closed

Crash during upload #2930

varungakhar opened this issue Jan 2, 2023 · 3 comments · Fixed by #2939 or #2998
Labels
bug Something isn't working

Comments

@varungakhar
Copy link

Describe the bug

When i try to upload a file . Server Crash . Please see attached screenshot

This issue may be related to vapor/multipart-kit#86

To Reproduce

Steps to reproduce the behavior:

  1. Upload a file larger than 1 mb

Environment

  • Vapor Framework version: 4.67.0
  • Vapor Toolbox version:
  • OS version: Mac Mini M1 (Ventura 13.0)
    Screenshot 2022-12-03 at 6 29 51 PM
    Screenshot 2022-12-03 at 6 30 02 PM

Additional context

Add any other context about the problem here.

@varungakhar varungakhar added the bug Something isn't working label Jan 2, 2023
@0xTim
Copy link
Member

0xTim commented Jan 2, 2023

What does the request you're sending to the server look like and what does your route handler code look like?

@varungakhar
Copy link
Author

Rite now I am using latest version of vapor (4.67.5) . Now I am getting race condition error too. Please check screenshot

Environment

Vapor Framework version: 4.67.5
OS version: Mac Mini M1 (Ventura 13.0)

Here's server code

    struct StreamController: RouteCollection 
    {
        func boot(routes: RoutesBuilder) throws 
        {
       let group = routes.grouped("api")
       group.on(.POST, "testingasyncstream", body: .stream, use: testingasyncstream)
        }

      func testingasyncstream(req:Request) async throws -> String
      {
        var buffer = ByteBuffer()
        for try await bytebuffer in req.body
        {
          var nextbuffer = bytebuffer
          req.logger.info("readableBytes...\(nextbuffer.readableBytes)")
          buffer.writeBuffer(&nextbuffer)
         }
        return ""
       }
   }

Here's iOS Code

 func uploadstreamfiles<T>(url:String,upload_type:String,random:Int,filemodel:T , clouser : @escaping (Data?,Error?) -> (Void) = {_,_ in}) where T : FilePro
                {
  let url = URL(string: url)!
  var request = URLRequest(url: url)
  request.httpMethod = "POST"
  request.setValue("multipart/form-data; boundary=boundary", forHTTPHeaderField: "Content-Type")
  request.setValue("chunked", forHTTPHeaderField: "Transfer-Encoding")
  var param = [String:Any]()
    let mirror = Mirror(reflecting: filemodel)
         for child in mirror.children
         {
             if (child.value is Data) == false
                 {
                     param[child.label!] = child.value
                  }
          } 
                var body = Data()
 if param.count > 0
 {
  for (key,value) in param
   {
      let unwrapvalue = unwrap(any: value)
       if let unwrapvalue = unwrapvalue
       {
        body.append("--boundary\r\n".data(using: .utf8)!)
        body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: .utf8)!)
        body.append("\(unwrapvalue)\r\n".data(using: .utf8)!)
        }
     }
 }
     if filemodel.image_file != nil
     {
body.append("--boundary\r\n".data(using: .utf8)!)
body.append("Content-Disposition: form-data; name=\"image_file\"; filename=\"image.jpeg\"\r\n".data(using: .utf8)!)
body.append("Content-Type: image/jpeg\r\n\r\n".data(using: .utf8)!)
body.append(filemodel.image_file!)
body.append("\r\n".data(using: .utf8)!)
       }
      if filemodel.video_file != nil
      {
body.append("--boundary\r\n".data(using: .utf8)!)
body.append("Content-Disposition: form-data; name=\"video_file\"; filename=\"video.mp4\"\r\n".data(using: .utf8)!)
body.append("Content-Type: video/mp4\r\n\r\n".data(using: .utf8)!)
body.append(filemodel.video_file!)
body.append("\r\n".data(using: .utf8)!)
       }
               
                body.append("--boundary--\r\n".data(using: .utf8)!)
                request.httpBody = body

                let uploadtask = mediasession.uploadTask(withStreamedRequest: request)
                uploadtask.taskDescription = String(random)
                uploadtask.resume()
}

Screenshot 2023-01-05 at 1 27 33 PM
Screenshot 2023-01-05 at 1 31 31 PM

@Joannis
Copy link
Member

Joannis commented Jan 19, 2023

@varungakhar can you try submitting this in a PR as a unit test, if that's possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants