Bug 4269 - A new thread from threadpool cannot be named
Summary: A new thread from threadpool cannot be named
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 2.10.x
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-04-05 12:48 UTC by haohaolee
Modified: 2012-04-09 04:12 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description haohaolee 2012-04-05 12:48:03 UTC
reproduce:

using System;
using System.Threading;

public class Example {
    public static void Main() {
        // Queue the task.
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

        Console.WriteLine("Main thread does some work, then sleeps.");
        Thread.Sleep(1000);

        Console.WriteLine("Main thread exits.");
    }

    static void ThreadProc(Object stateInfo) {
        Thread.CurrentThread.Name = "My Worker"; // there will be an exception here

        Console.WriteLine("Hello from the thread pool.");
    }
}

======================
dmcs thread.cs 
mono thread.exe

Main thread does some work, then sleeps.

Unhandled Exception: System.InvalidOperationException: Thread.Name can only be set once.
  at (wrapper managed-to-native) System.Threading.Thread:SetName_internal (System.Threading.InternalThread,string)
  at System.Threading.Thread.set_Name (System.String value) [0x00000] in <filename unknown>:0
  at Example.ThreadProc (System.Object stateInfo) [0x00000] in <filename unknown>:0

========================

Expected behavior: No exception and the thread name is set. Because this is the behavior of Microsoft .Net.
Comment 1 Zoltan Varga 2012-04-09 02:37:56 UTC
Fixed in master/2.10.
Comment 2 haohaolee 2012-04-09 03:33:09 UTC
Thanks for the quick fix.

i want to add some comments. The thread from the threadpool not only can be named, but also is null at first, according to the MS .net behaviors.

that means:

static void ThreadProc(Object stateInfo) {

    Debug.Assert( Thread.CurrentThread.Name == null );
    Thread.CurrentThread.Name = "My Worker"; 

    Console.WriteLine("Hello from the thread pool.");
}

Have seen your fix, I'm not sure if it makes sure that the thread's name is null at the beginning. Currently its name is "Threadpool worker"
Comment 3 Zoltan Varga 2012-04-09 04:12:17 UTC
Mono is different in this regard from .net, we give initial names to these threads. This seems useful, so I left it as it is.