Race condition initializing mime types

Issue #36 duplicate
Anonymous created an issue

I was uploading a bunch of images in parallel, and for some reason, some were correctly set with image/jpeg and others were left in application/octet-stream.

The issue is in Mimetypes.getInstance().

The first thread comes in and mimetypes is null so it starts constructing it. The first thing it does is set mimetypes to be empty.

Meanwhile the second thread comes in, and gets the empty mimetypes instance. Since it is not populated yet, it returns the default, "application/octet-stream".

The fix (w/o requiring synchronization) is easy, assign the new Mimetypes to a temporary local variable, and then assign it to the real singleton mimetypes at the very end. That way, worst case scenario is that multiple threads end up loading the mimetypes singleton, and the last in will just win, with the others discarded.

Comments (2)

  1. Log in to comment