M
M
MADm2015-02-19 13:50:33
ASP.NET
MADm, 2015-02-19 13:50:33

Out of order page requests, browser or server bug?

I work with web forms asp.net. I'm making a captcha page. The captcha image is generated in the page_load of the page, written to the session and returned in response to a request from this page. In some cases (for example, spam in the f5 browser), the session and the user have different data. For the test, I made a page in which I indicated public int a; in page_load indicated

if (Session["a"] == null)
                Session["a"] = a;
            else { 
                Session["a"] = (int)Session["a"] + 1;
                a = (int)Session["a"];
            }

in the page displayed the parameter to the user <%=a %>
those with each f5 the user should see the parameter by 1 more. but if we press f5, then the user will have 7 and in session 9. If I understand correctly, the browser discards the last 2 requests because request number 7 has already been loaded and its data to be sent is identical to requests 8 and 9. But the server still processes them. Is that the case or is it completely different?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Olga, 2015-02-19
@Liatano

Maybe it's all true. You can hang up an Ajax request as a test (or a crutch) to finalize the session.

S
Sergey Rodyushkin, 2015-02-20
@SergeyRodyushkin

And in my opinion, there is a banal race condition. When we hold down F5, several requests can theoretically enter the processing pipeline at the same time.
Try putting the specified operations in a critical section:

lock (Session.SyncRoot) {
    if (Session["a"] == null)
        Session["a"] = a;
    else { 
        Session["a"] = (int)Session["a"] + 1;
        a = (int)Session["a"];
    }
}

And I would do this, otherwise you have three read operations, although only one is needed:
var storedValue = Session["a"];
a = (storedValue == null) ? a : (int)storedValue + 1;
Session["a"] = a;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question