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

Precondition failure when write request body into file asyncronously. #2985

Open
alex1704 opened this issue Mar 21, 2023 · 10 comments
Open

Precondition failure when write request body into file asyncronously. #2985

alex1704 opened this issue Mar 21, 2023 · 10 comments
Labels
bug Something isn't working

Comments

@alex1704
Copy link

Describe the bug

I'm implementing upload functionality with HTTPBodyStreamStrategy == .stream. Inside request handler i'm writing data to file in chunks ayncronously. When large file is uploaded precondition failure is executed.

Here is my implementation:

import Vapor
import NIO

func routes(_ app: Application) throws {
    app.on(.POST, "upload", body: .stream, use: { req async throws in
        var offset: Int64 = 0
        let uploads = req.application.directory.workingDirectory.appending("uploads")
        let outPath = "\(uploads)/file"
        try? FileManager.default.createDirectory(atPath: uploads, withIntermediateDirectories: false)
        try? FileManager.default.removeItem(atPath: outPath)
        let fileHandle = try NIOFileHandle(path: outPath, mode: .write, flags: .allowFileCreation())

        for try await buffer in req.body {
            try await req.application.fileio.write(fileHandle: fileHandle,
                                                   toOffset: offset,
                                                   buffer: buffer,
                                                   eventLoop: req.eventLoop).get()
            offset += Int64(buffer.readableBytes)
        }

        try fileHandle.close()
        return "ok"
    })
}

To Reproduce

  1. create large file (600M): mkfile -n 600m file
  2. curl -X POST 'http://localhost:8080/upload' --data-binary '@file'
  3. see error

error

Also i noticed that error appear after repeating curl request several times on smaller files, like 100M.

Expected behavior

No error should happen.
If i use futures implementation, everything works fine.

Environment

  • Vapor Framework version: 4.74.1
  • Vapor Toolbox version: 18.6.0
  • OS version: macOS 13.2 (22D49)
@alex1704 alex1704 added the bug Something isn't working label Mar 21, 2023
@0xTim 0xTim closed this as completed Nov 11, 2023
@0xTim
Copy link
Member

0xTim commented Nov 11, 2023

This should now be fixed, feel free to reopen if not!

@alex1704
Copy link
Author

alex1704 commented Nov 11, 2023

@0xTim, still can reproduce. I created hello world project with upload endpoint. Additionally added bigFileTest.sh script to replicate the issue.
My current environment:

  • Vapor:
    • framework: 4.86.2
    • toolbox: 18.7.4
  • OS: 13.6 (22G120)
    helloVapor.zip

@0xTim 0xTim reopened this Nov 11, 2023
@tib
Copy link

tib commented Feb 5, 2024

I can confirm, this bug still exists in Vapor 4.92.1, any ideas how to fix it? 🤔

@0xTim
Copy link
Member

0xTim commented Feb 6, 2024

@tib does the async-streaming-body-client-disconnect branch fix it?

@hsharghi
Copy link
Contributor

@tib does the async-streaming-body-client-disconnect branch fix it?

Can't find the branch

@0xTim
Copy link
Member

0xTim commented Feb 14, 2024

@hsharghi we've just released https://github.com/vapor/vapor/releases/tag/4.92.3 which has the fix in it

@0xTim
Copy link
Member

0xTim commented Feb 14, 2024

@alex1704 do you want to confirm it's fixed?

@alex1704
Copy link
Author

@0xTim sure, will test it later.

@hsharghi
Copy link
Contributor

@0xTim The issue still exists

Vapor/RequestBody+Concurrency.swift:34: Fatal error

preconditionFailure()              NIO-SGLTN-0-#5 (10): Fatal error

@alex1704
Copy link
Author

yep, can confirm hsharghi message, bug still persists in 4.92.3 version.

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
Development

No branches or pull requests

4 participants