Re: Session file locking not working properly in multithreaded environment

Tim Roberts <[email protected]> Fri, 10 May 2019 09:33:23 -0700
Newsgroups gmane.comp.python.cherrypy
Organization Providenza & Boekelheide, Inc.
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------D0CF436EE7ED2A2E28A4E1C6
Content-Type: text/plain; charset="UTF-8"; format=flowed
Content-Transfer-Encoding: quoted-printable

Ralph Heinkel wrote:
>
> The first two requests looked entirely ok, the lock was acquired, the=20
> session was loaded, then after the request the session got saved, and=20
> the lock released.
>
> But then suddenly two threads obtain a lock to the same file! How is=20
> this possible? I have to mention that this does not happen every time=20
> I load the main page, sometimes I have to reload 30 or 40 times until=20
> this behavior shows up.
>
> Looking at the code I have no idea how this can happen, the code (it=20
> uses |fcntl.||flock()with correct flags) looks entirely OK to me.
> |

Ah, you've stumbled across one of the classic blunders, as they say in=20
Princess Bride.=C2=A0 The Unix "flock" call only blocks simultaneous access=
=20
between different processes.=C2=A0 It doesn't protect multiple threads with=
in=20
a single process.=C2=A0 A single process can call flock repeatedly on a=20
single file, and all of the calls will succeed.

If you want to protect a resource between threads of a single process,=20
you need to use something other than flock, like a=C2=A0 Lock from the=20
"threading" module.

--=20
Tim Roberts, [email protected]
Providenza & Boekelheide, Inc.

--=20
You received this message because you are subscribed to the Google Groups "=
cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
Visit this group at https://groups.google.com/group/cherrypy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/=
cherrypy-users/1bb257d1-4160-0e4b-4621-4575fa3f3bc7%40probo.com.
For more options, visit https://groups.google.com/d/optout.

--------------D0CF436EE7ED2A2E28A4E1C6
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<html>
  <head>
    <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUTF-8=
">
  </head>
  <body text=3D"#000000" bgcolor=3D"#FFFFFF">
    <div class=3D"moz-cite-prefix">Ralph Heinkel wrote:<br>
    </div>
    <blockquote type=3D"cite"
      cite=3D"mid:5460092d-e7e7-49a3-974d-d3cdc1c05885-/[email protected]">
      <meta http-equiv=3D"content-type" content=3D"text/html; charset=3DUTF=
-8">
      <div dir=3D"ltr"><br>
        The first two requests looked entirely ok, the lock was
        acquired, the session was loaded, then after the request the
        session got saved, and the lock released. <br>
        <br>
        But then suddenly two threads obtain a lock to the same file!
        How is this possible? I have to mention that this does not
        happen every time I load the main page, sometimes I have to
        reload 30 or 40 times until this behavior shows up.<br>
        <br>
        Looking at the code I have no idea how this can happen, the code
        (it uses <code class=3D"descclassname">fcntl.</code><code
          class=3D"descname">flock()<span style=3D"font-family:
            arial,sans-serif;"> with correct flags) looks entirely OK to
            me. <br>
          </span></code></div>
    </blockquote>
    <p>Ah, you've stumbled across one of the classic blunders, as they
      say in Princess Bride.=C2=A0 The Unix "flock" call only blocks
      simultaneous access between different processes.=C2=A0 It doesn't
      protect multiple threads within a single process.=C2=A0 A single
      process can call flock repeatedly on a single file, and all of the
      calls will succeed.</p>
    <p>If you want to protect a resource between threads of a single
      process, you need to use something other than flock, like a=C2=A0 Loc=
k
      from the "threading" module.<br>
    </p>
    <pre class=3D"moz-signature" cols=3D"72">--=20
Tim Roberts, <a class=3D"moz-txt-link-abbreviated" href=3D"mailto:timr@prob=
o.com">[email protected]</a>
Providenza &amp; Boekelheide, Inc.
</pre>
  </body>
</html>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;cherrypy-users&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:cherrypy-users+unsubscribe-/[email protected]">cher=
rypy-users+unsubscribe-/[email protected]</a>.<br />
To post to this group, send email to <a href=3D"mailto:cherrypy-users@googl=
egroups.com">cherrypy-users-/[email protected]</a>.<br />
Visit this group at <a href=3D"https://groups.google.com/group/cherrypy-use=
rs">https://groups.google.com/group/cherrypy-users</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/d/msgid/cherrypy-users/1bb257d1-4160-0e4b-4621-4575fa3f3bc7%40probo.com?=
utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/d/msgid/c=
herrypy-users/1bb257d1-4160-0e4b-4621-4575fa3f3bc7%40probo.com</a>.<br />
For more options, visit <a href=3D"https://groups.google.com/d/optout">http=
s://groups.google.com/d/optout</a>.<br />

--------------D0CF436EE7ED2A2E28A4E1C6--