Skip to content

dart:io File readAsBytes() doesn't handle large files #35890

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

Closed
sortie opened this issue Feb 8, 2019 · 3 comments
Closed

dart:io File readAsBytes() doesn't handle large files #35890

sortie opened this issue Feb 8, 2019 · 3 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@sortie
Copy link
Contributor

sortie commented Feb 8, 2019

dart:io's File class doesn't handle large files (files at least 2 GIB) properly in readAsBytes() and (readAsBytesSync() has related if different problems). Instead it attempts to return an invalid array of negative size (-4096).

For instance, make a big file:

truncate -s 4387733994 big

Then this program shows the problem:

import 'dart:io';

main() async {
  print(await new File("big").readAsBytes());
}

Which fails like this:

tcmalloc: large alloc 4387741696 bytes == 0x55ded9554000 @  0x55ded653caa7 0x55ded6544819 0x55ded652ad41 0x55ded67ba171 0x55ded67b713a 0x55ded67b78cc 0x55ded6904e3d 0x55ded6904cd4 0x55ded6860019 0x7ff96cfa2494
../../runtime/vm/object.cc: 22026: error: Fatal error in ExternalTypedData::New: invalid len -4096

Dumping native stack trace for thread 7e72
  [0x000055ded6863a2f] dart::Profiler::DumpStackTrace(void*)
  [0x000055ded6863a2f] dart::Profiler::DumpStackTrace(void*)
  [0x000055ded6a993b2] dart::Assert::Fail(char const*, ...)
  [0x000055ded68400c0] dart::ExternalTypedData::ToCString() const
  [0x000055ded687e4c6] dart::ExternalTypedData::ReadFrom(dart::SnapshotReader*, long, long, dart::Snapshot::Kind, bool)
  [0x000055ded68e3943] dart::SnapshotReader::ReadObjectImpl(long, bool, long, long)
  [0x000055ded68e5a6c] dart::SnapshotReader::ArrayReadFrom(long, dart::Array const&, long, long)
  [0x000055ded687cee0] dart::Array::ReadFrom(dart::SnapshotReader*, long, long, dart::Snapshot::Kind, bool)
  [0x000055ded68e3e0c] dart::SnapshotReader::ReadObjectImpl(long, bool, long, long)
  [0x000055ded68e26f7] dart::SnapshotReader::ReadObject()
  [0x000055ded6785139] dart::IsolateMessageHandler::HandleMessage(dart::Message*)
  [0x000055ded67b713a] dart::MessageHandler::HandleMessages(dart::MonitorLocker*, bool, bool)
  [0x000055ded67b78cc] dart::MessageHandler::TaskCallback()
  [0x000055ded6904e3d] dart::ThreadPool::Worker::Loop()
  [0x000055ded6904cd4] dart::ThreadPool::Worker::Main(unsigned long)
  [0x000055ded6860019] Unknown symbol
-- End of DumpStackTrace
@sortie sortie added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Feb 8, 2019
@sortie sortie self-assigned this Feb 8, 2019
@eikob
Copy link

eikob commented Jan 11, 2021

Any news on this?

Here's a quick workaround to circumvent the crash (OS Error: Invalid argument, errno = 22):

  final file = File('somefile');
  final length = file.lengthSync();
  final bytes = Uint8List(length);
  final raFile = file.openSync();
  int pos = 0;
  const chunkSize = 1000000000;
  while (pos < length) {
    raFile.readIntoSync(bytes, pos, (pos+chunkSize).clamp(0, length));
    pos += chunkSize;
  }
  raFile.closeSync();

Used to replace readSync(), but it would probably work with the async methods as well.

@2S-Software
Copy link

Dart can't read large files (over 500 MB), which is very cumbersome for desktop applications.
Is there anyone who can help and fix this?

@brianquinlan
Copy link
Contributor

This is a duplicate of #51071, which has a fix in progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants