yourlabs / django-autocomplete-light

A fresh approach to autocomplete implementations, specially for Django. Status: v4 alpha, v3 stable, v2 & v1 deprecated.
https://django-autocomplete-light.readthedocs.io
MIT License
1.79k stars 467 forks source link

Initial values race condition #1292

Open al-muammar opened 2 years ago

al-muammar commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.

jpic commented 1 year ago

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

al-muammar commented 1 year ago

@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.

jpic commented 1 year ago

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