4

I have a ASP.NET web application that allows end users to upload a file. Once the file is on the server, I spawn a thread to process the file. The thread is passed data regarding the specific operation (UserId, file path, various options, etc.). Most of the data is passed around via objects and method parameters but UserId needs to be available more globally so I put it in thread-local storage.

The thread is lengthy but it just processes the file and aborts. Is my use of the named data slot safe in this circumstance? If UserA uploads a file then UserB uploads a file while the first file is still processing, is it possible that the thread for UserA will also be delegated to handle UserB, thus producing a conflict for the named slot? (i.e. The slot gets overwritten with UserB's id and the rest of the operation of UserA's file is linked to the wrong User, UserB).

Public Class FileUploadProcess
    Public UserId as String

    Public Sub ExecuteAsync()
        Dim t As New Thread(New ThreadStart(AddressOf ProcessFile))
        t.Start()
    End Sub

    Protected Sub ProcessFile()
        Dim slot As LocalDataStoreSlot = Thread.GetNamedDataSlot("UserId")
        Thread.SetData(slot, UserId)

        'lengthy operation to process file

        Thread.FreeNamedDataSlot("UserId")
        Thread.CurrentThread.Abort()
    End Sub
End Class

Note that I am not asking if the LocalNamedDataStore slots are thread-safe. By definition, I know that they are.

3 Answers 3

5

In this case your use of thread local storage is safe. No two threads will ever share the same local storage (hence it's thread local). So there is no chance that two concurrent requests will stomp on the others data.

Couple of other comments though

  • Do avoid the use of Thread.Abort. It's a very dangerous operation and truthfully not needed here. The thread will end the statement afterwards.
  • A better approach would be to create a class which contains the background operation that has the UserId as a local field. Each request gets a new class instance. This is a much easier way to pass the data around to the background tasks
7
  • Is there a possibility that one thread can handle more than one file at a time? Sorry if you answered that or if that doesnt make sense, but multi-threading in practice is a little beyond me.
    – Jeff
    Nov 29, 2011 at 20:26
  • @Jeff given the brief code sample I'd say no. It appears every thread is dedicated to a single file
    – JaredPar
    Nov 29, 2011 at 20:29
  • @JaredPar Thanks. The code sample contains most (if not all) the thread related code.
    – Jeff
    Nov 29, 2011 at 20:38
  • @JaredPar Re passing around a local instance with a UserId field: That was my first thought. Most of classes and methods related to processing the file don't really care about the user, but a few of them do. I don't like the idea of "polluting" the API of all of these classes & methods just to get to the UserId. In some cases the method that needs the UserId is 15+ steps up the stack from the ProcessFile method.
    – Jeff
    Nov 29, 2011 at 20:39
  • 1
    @Jeff but putting it in a thread local slot doesn't change the dependency, it just hides it.
    – JaredPar
    Nov 29, 2011 at 20:50
2

This is a safe operation.

I have to say that I that JaredPars opinion that it would be better to create a class and store the userid in that class as a field is incomplete to say the least.

Where do you then store that object? Since it is created per request you have to store it somewhere. Do you couple the page with this functionality? I wouldn't. Do you store in the Context.Items collection? That is a possibility but what do you do with unit tests where you are trying to abstract the code away from ASP.Net so it will be more testable?

I have personally done a hybrid of the two approaches: I create a single class that will contain all of the data elements that are request specific then I cache that object in Thread Local Storage. This allows the code to run in unit test frameworks without having to mock the ASP.Net runtime environment.

Another important point is this: if you intend to use asynchronous patterns in ASP.Net you should be aware that TLS is not forward to new threads when switching the execution context to a new thread. It is truly "Thread local".

1
  • 1
    Thanks Brian. There's a few good points here to ponder over. When would you access the cached object via TLS versus passing the data around via method parameters?
    – Jeff
    Nov 30, 2011 at 14:25
0

It is safe for the time being, but be careful if performing async operations on that thread that may run on other threads. Those other threads won't have access to the origin threads TLS. A "safer" option that will allow you to use async calls in future is to store the user id in an AsyncLocal which is context that will flow with any async tasks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.