Please save me from shooting myself in the head!

Discussion in 'ASP .Net' started by =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Oct 18, 2005.

  1. I'm dying here... I have an object that I use to access objects in the
    session state. I may be reading and writing at the same time, so I am trying
    to make the accessors thread safe using ReaderWriter locks. Here's a
    stripped down example of my class:

    Class SessionLogic{
    private static ReaderWriterLock myObjectLock = new ReaderWriterLock();

    ....

    public MyObject MyObject{
    get
    {
    MyObject foo = null;
    myObjectLock.AcquireReaderLock(5000);
    try
    {
    foo = this.session["MyObject"] as MyObject;
    try
    {
    LockCookie monster = myObjectLock.UpgradeToWriterLock(5000);
    try
    {
    foo = new MyObject();
    this.session["MyObject"] = foo;
    }
    finally
    {
    myObjectLock.DowngradeFromWriterLock(ref monster);
    }
    }
    catch (Exception ex)
    {
    string message = ex.Message; // just so I can see
    }
    finally
    {
    myObjectLock.ReleaseReaderLock();
    }
    return foo;
    }
    }
    }

    This structure is almost word for word out of MS' documentation on
    ReaderWriterLock. Yet whenever I run it, I get an error: "Attempt to
    release mutex not owned by caller." What the... Its the ONLY CALLER! What
    is happening here????
    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Oct 18, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=

    Bruce Barker Guest

    not sure why you are doing this. add/getting references from session are
    thread safe. so there is no need for the locking code. also asp.net
    serializes access to the session by only running one request at a time for
    the same session.

    now to the problem with your code, you use a global lock so any thread
    accessing the session object (even though the session objects are different
    for threads processing diffent sessions) must take a lock and release. the
    lock is created under the creditials of the user of the first request, so
    other users will not be able to access it unless you change the security of
    the mutex, to allow read/write from anyone. turn off authenication to see
    this.

    -- bruce (sqlwork.com)


    "William Sullivan" <> wrote in
    message news:...
    > I'm dying here... I have an object that I use to access objects in the
    > session state. I may be reading and writing at the same time, so I am
    > trying
    > to make the accessors thread safe using ReaderWriter locks. Here's a
    > stripped down example of my class:
    >
    > Class SessionLogic{
    > private static ReaderWriterLock myObjectLock = new ReaderWriterLock();
    >
    > ...
    >
    > public MyObject MyObject{
    > get
    > {
    > MyObject foo = null;
    > myObjectLock.AcquireReaderLock(5000);
    > try
    > {
    > foo = this.session["MyObject"] as MyObject;
    > try
    > {
    > LockCookie monster = myObjectLock.UpgradeToWriterLock(5000);
    > try
    > {
    > foo = new MyObject();
    > this.session["MyObject"] = foo;
    > }
    > finally
    > {
    > myObjectLock.DowngradeFromWriterLock(ref monster);
    > }
    > }
    > catch (Exception ex)
    > {
    > string message = ex.Message; // just so I can see
    > }
    > finally
    > {
    > myObjectLock.ReleaseReaderLock();
    > }
    > return foo;
    > }
    > }
    > }
    >
    > This structure is almost word for word out of MS' documentation on
    > ReaderWriterLock. Yet whenever I run it, I get an error: "Attempt to
    > release mutex not owned by caller." What the... Its the ONLY CALLER!
    > What
    > is happening here????
    >
    Bruce Barker, Oct 18, 2005
    #2
    1. Advertising

  3. Thank you, my head now remains whole.

    "Bruce Barker" wrote:

    > not sure why you are doing this. add/getting references from session are
    > thread safe. so there is no need for the locking code. also asp.net
    > serializes access to the session by only running one request at a time for
    > the same session.
    >
    > now to the problem with your code, you use a global lock so any thread
    > accessing the session object (even though the session objects are different
    > for threads processing diffent sessions) must take a lock and release. the
    > lock is created under the creditials of the user of the first request, so
    > other users will not be able to access it unless you change the security of
    > the mutex, to allow read/write from anyone. turn off authenication to see
    > this.
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "William Sullivan" <> wrote in
    > message news:...
    > > I'm dying here... I have an object that I use to access objects in the
    > > session state. I may be reading and writing at the same time, so I am
    > > trying
    > > to make the accessors thread safe using ReaderWriter locks. Here's a
    > > stripped down example of my class:
    > >
    > > Class SessionLogic{
    > > private static ReaderWriterLock myObjectLock = new ReaderWriterLock();
    > >
    > > ...
    > >
    > > public MyObject MyObject{
    > > get
    > > {
    > > MyObject foo = null;
    > > myObjectLock.AcquireReaderLock(5000);
    > > try
    > > {
    > > foo = this.session["MyObject"] as MyObject;
    > > try
    > > {
    > > LockCookie monster = myObjectLock.UpgradeToWriterLock(5000);
    > > try
    > > {
    > > foo = new MyObject();
    > > this.session["MyObject"] = foo;
    > > }
    > > finally
    > > {
    > > myObjectLock.DowngradeFromWriterLock(ref monster);
    > > }
    > > }
    > > catch (Exception ex)
    > > {
    > > string message = ex.Message; // just so I can see
    > > }
    > > finally
    > > {
    > > myObjectLock.ReleaseReaderLock();
    > > }
    > > return foo;
    > > }
    > > }
    > > }
    > >
    > > This structure is almost word for word out of MS' documentation on
    > > ReaderWriterLock. Yet whenever I run it, I get an error: "Attempt to
    > > release mutex not owned by caller." What the... Its the ONLY CALLER!
    > > What
    > > is happening here????
    > >

    >
    >
    >
    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Oct 19, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Meade
    Replies:
    4
    Views:
    376
    Rob Meade
    Dec 3, 2003
  2. Andrea Maschio
    Replies:
    0
    Views:
    708
    Andrea Maschio
    Dec 5, 2003
  3. Sean Kenwrick

    [OT] Shooting yourself in the foot

    Sean Kenwrick, Jan 30, 2004, in forum: C Programming
    Replies:
    11
    Views:
    556
  4. coolgirl
    Replies:
    0
    Views:
    1,291
    coolgirl
    Nov 5, 2010
  5. Miloud Kabsh

    trouble shooting/debugging asp hang

    Miloud Kabsh, Mar 4, 2004, in forum: ASP General
    Replies:
    2
    Views:
    93
    Miloud Kabsh
    Mar 6, 2004
Loading...

Share This Page