Skip to content

ByteData Serialization Bug (Image Upload) #3213

Description

@Lightgaming

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

Activity

  1. added
    bug
    on Feb 9, 2025
  2. Lightgaming commented on Feb 10, 2025

    @Lightgaming
    Author

    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.

    Future<bool> uploadImage(Session session, String path, ByteData data) async {
        print(data);
        return true;
      }
    
  3. changed the title [-]Local Image Upload sometimes does not work[/-] [+]ByteData Serialization Bug (Image Upload)[/+] on Feb 10, 2025
  4. added
    area: endpoints
    on Feb 11, 2025
  5. SandPod commented on Feb 11, 2025

    @SandPod
    Contributor

    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?

  6. Lightgaming commented on Feb 13, 2025

    @Lightgaming
    Author

    @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.

  7. Lightgaming commented on Feb 13, 2025

    @Lightgaming
    Author

    Here is a Video of me demonstrating an upload with two .jpg Files

    20250213-1002-43.8486201.mp4
  8. self-assigned this
    on Mar 6, 2025
  9. moved this from Next 🌟 to Now 🔥 in Serverpod Frameworkson Mar 6, 2025
  10. nielsenko commented on Mar 6, 2025

    @nielsenko
    Collaborator

    @Lightgaming You are getting a SocketError from the OS, hence Serverpod never manages to setup TCP connection, let alone a HttpConnection on top.

    Image

    Notice the 10054 error code from Windows. Are you by any chance running Windows Defender or similar?

  11. Lightgaming commented on Mar 6, 2025

    @Lightgaming
    Author

    @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"

  12. nielsenko commented on Mar 6, 2025

    @nielsenko
    Collaborator

    @Lightgaming A couple of questions:

    • Is it consistent what files fails, and which ones doesn't?
    • If so, can you attach a file that fails?
  13. Lightgaming commented on Mar 6, 2025

    @Lightgaming
    Author

    @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:

    // Endpoint 
    Future<bool> uploadByteData(Session session, String path, ByteData data) async {
        print(data);
        return true;
    }
    // Client
    // filer picker package used (file_picker: ^8.0.0)
    TextButton(
        onPressed: () async {
            final file = await FilePicker.platform.pickFiles(
                withData: true,
                type: FileType.image,
             );
             if (file == null) return;
             final ByteData byteData = ByteData.view(file.files.first.bytes.buffer);
              
             final test = await client.images.uploadByteData('test', byteData);
         },
        child: Text('Test'),
    ),
  14. nielsenko commented on Mar 7, 2025

    @nielsenko
    Collaborator

    @Lightgaming I have recreated the issue. We will look into what is happening.

  15. nielsenko commented on Mar 7, 2025

    @nielsenko
    Collaborator

    This is related to dart-lang/sdk#60271. I will implement a work-around.

  16. nielsenko commented on Mar 7, 2025

    @nielsenko
    Collaborator

    @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?

  17. Lightgaming commented on Mar 7, 2025

    @Lightgaming
    Author

    @nielsenko

    That was it! 🙌

    I had maxRequestSize set but wrong:

    Image

    Thank you so much for the help and your time!

  18. moved this from Now 🔥 to Done 🚀 in Serverpod Frameworkson Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: endpointsbug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions