Frontend and backend data Not in sync when two requests are made one after other in quick succession

Hi,

We have a web-console that run on rails, on the console we have a button
to add a new entry and below it we show all the entries that are already
present in a box. when we click on the button a request goes to the
backend to add that entry, when that request is complete a pop-up comes
with message that entry was successful or not. This feature has been
working fine. Of late we have observed a bug.
when we click on add a new entry and before that request is complete if
we double click on an entry in the box to view the details of another
entry, the pop-up still comes with success message but when go to the
backend to verify if the entry was made or NOT, we dont find the new
entry.

Can anyone throw light on what could the possible reason be.

This is my first post on this forum, hope i am not breaking any rule by
asking this question here.

Thanks,
Uday.

Have a look in the log (log/development.log assuming running in
development mode) to get more information about what is going on.
Also you can use various methods to debug your code in order to
understand the problem. See the Rails Guide on debugging if you are
not familiar with these techniques.

Colin

Hi,

I am fairly new to rails and ajax as well. I have hinch that the problem
i am facing is related to ajax requests. During the first ajax call,
adding a new entry to the data is requested but before this request is
complete another ajax call is made to the access the same dataset to
view one of the data content. I feel somewhere these two ajax calls gets
interleaved and the backend data is not updated to make the new entry
that was requested first. Please suggest a way to investigate how to
trace the ajax calls and if there is any possibility of this hunch might
be true.

Thanks,
Uday.

Hi,

I am fairly new to rails and ajax as well. I have hinch that the problem
i am facing is related to ajax requests. During the first ajax call,
adding a new entry to the data is requested but before this request is
complete another ajax call is made to the access the same dataset to
view one of the data content. I feel somewhere these two ajax calls gets
interleaved and the backend data is not updated to make the new entry
that was requested first. Please suggest a way to investigate how to
trace the ajax calls and if there is any possibility of this hunch might
be true.

It's entirely possible that overlapping ajax calls could trigger a
race condition. For example if your controller does

foo = Foo.find(params[:id])
# change some attributes
foo.save!

And you have 2 requests that come in at close to the same time then
the sequencing can look like

1: instance one does Foo.find
2: instance two does Foo.find
3: instance one updates and saves
4: instance two updates and saves, overwriting the changes made in 3.
if you have a feeling that this might be the case, one thing that I
sometimes find helpful is to insert a few sleeps into the app at
strategic points so as to make the race condition more likely.
Depending on what sort of stuff you are doing to your models, you may
find optimistic locking useful

Fred

Thanks Fred,
it was really helpful.
To prevent the issue i have disabled the 2nd ajax call from the
frontend.