Please save me from shooting myself in the head!

G

Guest

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????
 
B

Bruce Barker

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)
 
G

Guest

Thank you, my head now remains whole.

Bruce Barker said:
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 said:
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????
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top