django-autocomplete-light icon indicating copy to clipboard operation
django-autocomplete-light copied to clipboard

Initial values race condition

Open al-muammar opened this issue 3 years ago • 6 comments
trafficstars

Hi DAL team,

I have a problem that on opening a django admin page with DAL fields it doesn't have initial values, while making the same thing in Safari prepopulates all of the fields correctly. Also, when opening a new tab in Safari and pasting there the url, on the first load the initial values are empty, while after reloading they are prepopulated correctly.

My wild guess is that there is some race condition in JS code.

al-muammar avatar Apr 06 '22 04:04 al-muammar

Okay, I was able to create a clear repro of the problem https://user-images.githubusercontent.com/11528067/163552847-dd89b199-ff23-44bc-ac7e-4ea7282f6dec.mp4

Note that it only happens with the required fields. Optional fields doesn't get reset (see driver, it's also autocomplete).

@jpic , could you comment on it, please?

al-muammar avatar Apr 15 '22 09:04 al-muammar

Successful (initialized correctly) stack trace:

(anonymous) (jquery.js:6292)
domManip (jquery.js:6103)
after (jquery.js:6290)
jQuery.fn.<computed> (jquery.js:6398)
Select2._placeContainer (select2.full.js:5470)
Select2 (select2.full.js:5393)
(anonymous) (select2.full.js:6762)
each (jquery.js:381)
each (jquery.js:203)
$.fn.select2 (select2.full.js:6759)
(anonymous) (select2.js:92)
initialize (autocomplete_light.js:153)
each (jquery.js:381)
each (jquery.js:203)
(anonymous) (autocomplete_light.js:185)
mightThrow (jquery.js:3762)
process (jquery.js:3830)
setTimeout (async)
(anonymous) (jquery.js:3868)
fire (jquery.js:3496)
add (jquery.js:3555)
(anonymous) (jquery.js:3888)
Deferred (jquery.js:3979)
then (jquery.js:3873)
jQuery.fn.ready (jquery.js:4068)
(anonymous) (autocomplete_light.js:184)
(anonymous) (autocomplete_light.js:238)
load (async)
(anonymous) (autocomplete_light.js:30)

al-muammar avatar Apr 15 '22 10:04 al-muammar

I set a breakpoint inside autocomplete_light.js:185 (initialize) and here is the difference. When everything initializes successfully, I see the following picture (for m2m field): Screenshot 2022-04-15 at 11 22 19 While if the initialization is unsuccessful, the picture is as follows: Screenshot 2022-04-15 at 11 22 03

I.e. there is a race condition on whether the child is selected or not. If it is selected before initialization happens, initialization is successful, while if it happens after, the field is reset.

al-muammar avatar Apr 15 '22 10:04 al-muammar

Thanks for your report! Could you reproduce this with browsers other than safari?

jpic avatar Sep 09 '22 22:09 jpic

@jpic, yes, this bug even more frequent in Chrome. I specifically made a video with Safari because it was easy to hit refresh a couple of times and get the correct behaviour.

al-muammar avatar Sep 11 '22 19:09 al-muammar

Ok, thanks for your feedback!

I wonder if we still have the problem with this version: https://github.com/yourlabs/django-autocomplete-light/pull/1170

Easier is maybe to try with the serve.py script in the github.com/yourlabs/autocomplete-light repository

jpic avatar Sep 15 '22 19:09 jpic