creativescala / doodle

Compositional vector graphics in Scala / Scala.JS
https://creativescala.org/doodle/
Apache License 2.0
327 stars 75 forks source link

Write operation returns before being fully performed #129

Closed mprevel closed 1 year ago

mprevel commented 1 year ago

Hi,

I am building an image having a lot of components (including Image.rectangle, Image.circle, Image.closedPath, and Image.text), with customization (stroke color, fill color, translate, rotate, ...). Finally, I call the write operation myImage.write[Png](outputPngFile).

I encounter an issue when I try to read the file that has been created by this write operation just after. On some cases, the file does not exist yet, on other cases the png file is corrupted (I only get the top part of the image, or that's what is displayed when I open the png file in an editor). Sometimes it seems to have enough time to write all the data to the file so I get the expected png (usually, the first call to the function is OK, the following are not).

When I add a "sleep" after the write operation, as a workaround, the image is OK. Unfortunately, this does not ensure that the write operation is really done and it increases the response time of my function.

So, my understanding is that the write operation returns before the file is fully written. Is there a way to make it blocking until the end of the task or is there another function to call that would have such a behavior?

Regards,

noelwelsh commented 1 year ago

Interesting. I don't know why this is occurring, but I have a suggestion you can try. Instead of anImage.write[Png](file) you can try anImage.writeToIO[Png](file). This gives you can IO value you can then chain further operations on to.

I am surprised to see this bug, and I expect my suggestion above will not fix it. Calls to write end up calling unsafeRunSync on the JVM, so these operations should block until writing has finished. Perhaps the underlying calls to ImageIO are not synchronous. The documentation makes no mention of this, though, and it would be surprising behaviour. Another possibility is that the OS is not syncing the data to disk. Are these particularly large files?

mprevel commented 1 year ago

Thanks for your answer.

The image is not that big ~150-200kB and the app is running in a K8S pod with distributed storage, so it may be a problem of data sync I encounter because I try to read it as soon as it is produced.