sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Problem with concurrency on Windows (maybe)

Open proreact-mj opened this issue 10 years ago • 3 comments

Hi.

I think I might have found a problem with the concurrency handling on windows.

I have a script that I execute directly with node + a folder parameter. It scans the folder, reads the filelist, checks if a thumbnail exists and if not, call my convert function with the file path and creates 3 thumbs of the file using clone().

Running it on windows (decent Lenovo laptop with SSD) in a fairly small folder of 142 files, it results is a lot of write fails.

Error is always:

Unhandled rejection Error: vips__file_open_write: unable to open file "[result filepath]" for writing
windows error: Den angivne fil blev ikke fundet. (translate: file not found).

Logging console.log(sharp.counters()); show that the script immidiately jumps to { queue: 266, process: 160 } before the errors start to come in (queue callbacks to early?).

I've tried with sharp.concurrency(1); process.env.UV_THREADPOOL_SIZE = 1;

But no change.

Interestingly enough it is always the first files to be processed that fails and rerunning the script, clears the gap, because the process skips the files that completed successfully first time around

Process code:

var convert = function(filepath, image, i){
  var file = sharp(filepath);
  var med = file.clone();
  med.resize(200, 200).toFile(medium + image);
  var sma = file.clone();
  sma.resize(100, 100).toFile(small + image);
  var mi = file.clone();
  mi.resize(50, 50).toFile(mini + image);
  console.log(sharp.counters());
};

I've tried then'ing the resizes, but no change. Next step would be wrapping the convert call it in some kind of queue that I can manage myself, but I believe that shouldn't be necessary?

Here is the kicker. I cannot replicate the issue on my Ubuntu server (non-ssd). I ran the same script on a folder with 18000 files and it just chewed through it, leading me to suggest that it could be cause but the fast ssd disk on my workstation and windows choking on the "high" number of concurrent file_write operations?

Anyway. Loving the tool and this is obviously not an immediate problem for me, so just to let you know.

proreact-mj avatar Apr 21 '16 08:04 proreact-mj

Hello, sharp will process tasks on the queue when there's a "tick" of the event loop. If the thing that's calling your convert function is doing so in a synchronous manner (perhaps using fs.readDirSync?), then it won't get to do this until something yields (end of script reached?).

You might be hitting the 512 open file limit of Windows' C runtime library - http://stackoverflow.com/a/870224

lovell avatar Apr 21 '16 10:04 lovell

So, basically,

sharp.concurrency(1); 
process.env.UV_THREADPOOL_SIZE = 1;

just isn't doing what I thought it would.

512 open file limit seems like a reasonable explanation. I'll wrap the conversion in a queue for my bulk conversion. Thanks for the explanation and core issue reference.

proreact-mj avatar Apr 21 '16 10:04 proreact-mj

No problem. In summary:

  • sharp.concurrency(1); = number of libvips-managed threads to spawn per task.
  • process.env.UV_THREADPOOL_SIZE = 1; = number of libuv-managed threads to process queued tasks, which includes otherwise-blocking local file system operations - see http://stackoverflow.com/a/29472222/4625124

lovell avatar Apr 21 '16 12:04 lovell