Repository navigation
Description
Describe the bug
When I try to upload some images the storeFile Function will fail and Serverpod closes the connection to the Flutter Client.
I already increased the maxRequestSize to 2000000000000 - which should be plenty ^^
It also happens when I try to upload GIF files.
Since the storeFile Function returns void and Serverpod is not logging any Error Message I have trouble debugging the cause of this issue.
Client Error Message:
flutter: Failed call: images.uploadEventImage
flutter: HttpException: Connection closed before full header was received, uri = http://[redacted]:8080/images
[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: HttpException: Connection closed before full header was received, uri = http://[redacted]:8080/images
To reproduce
Here is the Code I use:
Client:
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.image,
allowMultiple: false,
withData: true,
);
if (result == null) return;
final url = await uploadImage(result.files.first);Future<String?> uploadImage(PlatformFile file) async {
if (file.bytes == null) {
debugPrint('File bytes is null');
return null;
}
final fileExists = await client.images.checkFileExists(file.name);
if (fileExists) {
final publicUrl = await client.images.getPublicImageUrl(file.name);
if (publicUrl != '') {
return publicUrl;
}
return null;
}
await client.images.uploadEventImage(
file.name,
file.bytes!.buffer.asByteData(),
);
final fileExistsAfterUpload = await client.images.checkFileExists(file.name);
if (fileExistsAfterUpload) {
final publicUrl = await client.images.getPublicImageUrl(file.name);
if (publicUrl != '') {
return publicUrl;
}
return null;
}
return null;
}Server:
Future<bool> checkFileExists(Session session, String path) async {
return await session.storage.fileExists(
storageId: 'public',
path: path,
);
}
Future<String> getPublicImageUrl(Session session, String path) async {
final uri = await session.storage.getPublicUrl(
storageId: 'public',
path: path,
);
return uri.toString();
}
Future<void> deleteImage(Session session, String path) async {
try {
return await session.storage.deleteFile(
storageId: 'public',
path: path,
);
} catch (e) {
print(e);
}
}
Future<void> uploadEventImage(
Session session, String path, ByteData data) async {
return await session.storage.storeFile(
storageId: 'public',
path: path,
byteData: data,
expiration: DateTime.now().add(Duration(days: 365)),
);
}Expected behavior
I would like to be able to upload all types of files using this endpoint and receive success/error feedback on the storeFile Function.
Library version
All of my Serverpod Dependencies use 2.3.1
Platform information
Tested Platform: Windows (Client & Server)
Additional context
No response
How experienced are you with this library?
Intermediate - Familiar with the basics or have used it in a few projects
Are you interested in working on a PR for this?
- I want to work on this
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity
Lightgaming commentedon Feb 10, 2025
This might have something to do with the Serialization of the ByteData Class because when I remove the storeFile Function call and just print the ByteData the Server throws the same error and does not allow me to debug it.
[-]Local Image Upload sometimes does not work[/-][+]ByteData Serialization Bug (Image Upload)[/+]SandPod commentedon Feb 11, 2025
Hey @Lightgaming 👋
Thanks for reporting the issue.
What versions of the flutter and file picker are you using?
Do you have this issue with a specific set of files or with all filetypes?
Lightgaming commentedon Feb 13, 2025
@SandPod
Thanks for being intrested in my issue!
I am using flutter 3.27.1 channel stable and file_picker 8.0.0
I have only tested images for now, but this issue does not seem to be correlated to the filetype since I have .jpg images I am able to upload and others with the same filetype I am not able to upload.
Lightgaming commentedon Feb 13, 2025
Here is a Video of me demonstrating an upload with two .jpg Files
20250213-1002-43.8486201.mp4
nielsenko commentedon Mar 6, 2025
@Lightgaming You are getting a SocketError from the OS, hence Serverpod never manages to setup TCP connection, let alone a HttpConnection on top.
Notice the 10054 error code from Windows. Are you by any chance running Windows Defender or similar?
Lightgaming commentedon Mar 6, 2025
@nielsenko
Thanks for being intrested in my issue!
Yes I am running Windows Defender (as everyone who uses Windows should).
I don't think it is an networking issue because the connection works for other images too - as shown in the 2nd upload in my demo video.
The Socket Exception is probably because the Serverpod Endpoint crashes internally or something. The Error message also has some extra information appended in german, which translates to:
"OS Error: The remote host has closed the running connection"
nielsenko commentedon Mar 6, 2025
@Lightgaming A couple of questions:
Lightgaming commentedon Mar 6, 2025
@nielsenko
The files that do not work consistently do not work but I haven't found a scheme yet on what type of images fail (sometimes png, sometimes jpeg, small, big)
I found some images with Creative Commons licence that work/do not.
This one does not work (https://freepngimg.com/thumb/orange/74593-button-testing-manual-computer-now-software.png)
and this one works (https://openclipart.org/image/2400px/svg_to_png/211364/Eye-Test-Chart-2015010409.png)
Here is the minimal Code example:
nielsenko commentedon Mar 7, 2025
@Lightgaming I have recreated the issue. We will look into what is happening.
nielsenko commentedon Mar 7, 2025
This is related to dart-lang/sdk#60271. I will implement a work-around.
nielsenko commentedon Mar 7, 2025
@Lightgaming To the best of my knowledge this can only happen if the uploaded request is too large. There is a bug (either in serverpod or dart:io - depending on view point) that prevents serverpod from reporting this correctly. You should get a 413, and the above PR ensures that.
However this report indicates that you have already increased the maxRequestSize in the config. Did you do that for all environments?
Lightgaming commentedon Mar 7, 2025
@nielsenko
That was it! 🙌
I had maxRequestSize set but wrong:
Thank you so much for the help and your time!
fix: Implement alternative (better) fix for serverpod#3213
fix: Implement alternative (better) fix for serverpod#3213