lokesh / lightbox2

THE original Lightbox script (v2).
http://lokeshdhakar.com/projects/lightbox2/
MIT License
6.17k stars 1.76k forks source link

Bug - Race condition on slow networks in Image.onload handler set in changeImage #629

Open olwimo opened 6 years ago

olwimo commented 6 years ago

Lightbox.prototype.start resets this.album every time a new album is opened. It always ends by calling Lightbox.prototype.changeImage, which sets an Image.onload handler that accesses this.album[imageNumber] after the image is loaded (async). If the network is slow, it is possible for the user to close the album and open a new one before the image is loaded. If the new album opened by the user has less images in it than the index of the first image clicked, this.album[imageNumber] will be undefined, and the attempts to access its properties will result in an error. If the index is within the length of the new album it won't crash, it'll just show the "old" image in the new lightbox (which I guess is not expected/wrong behavior).

I've created an example on jsfiddle, https://jsfiddle.net/olwimo/h633q2re/ I've created a PR with a proposed fix, https://github.com/lokesh/lightbox2/pull/628