Share via


Do not treat fibers as threads

TypeName

DoNotTreatFibersAsThreads

CheckId

CA2003

Category

Microsoft.Reliability

Breaking Change

Non Breaking

Cause

A managed thread is being treated as a Win32 thread.

Rule Description

Do not assume a managed thread is a Win32 thread - it is a fiber. The CLR will run managed threads as fibers on top of real threads owned by SQL. These threads will be shared across AppDomains and even databases in the SQL Server process. Using managed thread local storage will work, but you may not use unmanaged thread local storage or assume your code will run on the current OS thread again. Do not change settings like the thread's locale. Do not call CreateCriticalSection or CreateMutex via P/Invoke because they require the thread that enters a lock also exit the lock. Because this will not be the case when you use fibers, Win32 critical sections and mutexes will be useless in SQL. You may safely use most of the state on a managed System.Thread object, including managed thread local storage and the thread's current UI culture. However, for programming model reasons, you will not be able to change a thread's current culture when running in SQL ;this will be enforced through a new permission.

How to Fix Violations

Examine your usage of threads and alter your code accordingly.

When to Suppress Warnings

You should not suppress this rule.