perfview icon indicating copy to clipboard operation
perfview copied to clipboard

"A CLR Profiler has already been attached" after few run with ForceGC

Open bigbearzhu opened this issue 8 years ago • 6 comments

I used "HeapDump /ForceGC" directly to trigger GC on some process periodically. However I noticed that after it runs few times it start to throw errors like this: "A CLR Profiler has already been attached. You cannot attach another. (a process restart will fix)"

The problem is that I need keep the process running, e.g. w3wp process from iis. Is there anyway to find out why the profiler is still attached and how to detach it if possible?

bigbearzhu avatar Nov 16 '16 02:11 bigbearzhu

Seems that the w3wp process is still having EtwClrProfiler.dll loaded after HeapDump quits.

bigbearzhu avatar Nov 16 '16 02:11 bigbearzhu

@bigbearzhu you have to tear down the process, the dll once loaded in a certain way (i.e. from process startup) will always have it attached.

mjsabby avatar Nov 16 '16 03:11 mjsabby

It is not attached at process startup. It is attached when HeapDump runs and detached after it finishes. It succeeds for a few times, and then start to fail to detach for some reason. Had a look at CorProfilerTracer.cpp line 298: if ((MatchAnyKeywords & DetachKeyword) != 0 && !m_profilerLoadedAtStartup && !m_detaching) Could m_profilerLoadedAtStartup be true when detaching so it is not detached?

https://msdn.microsoft.com/en-us/library/dd997942(v=vs.100).aspx#attach_details

Within the target process, the procedure for loading a profiler at attach-time is similar to the procedure for loading a profiler at startup-time: The CLR CoCreates the given CLSID, queries for the ICorProfilerCallback3 interface, and calls the ICorProfilerCallback3::InitializeForAttach method.

bigbearzhu avatar Nov 16 '16 03:11 bigbearzhu

looked into the log of the EtwClrProfiler.dll. Seems it is getting error when trying to detach the profiler:

Creating new CorProfilerInstance
ClrProfiler Initializing
Registering the ETW provider
Initialize() returns 0
ProfilerControlCallback DoETWCommand IsEnabled 0x2 Level 0x5I64 MatchAny 0x0
DoETWCommand(IsEnabled=2, Level=5 Keywords=0x8000,0)
Detaching
Done Detaching Detaching ret = 0x80131363

while a successful detaching log is like this:

Creating new CorProfilerInstance
ClrProfiler Initializing
Registering the ETW provider
Initialize() returns 0
ProfilerControlCallback DoETWCommand IsEnabled 0x2 Level 0x5I64 MatchAny 0x0
DoETWCommand(IsEnabled=2, Level=5 Keywords=0x8000,0)
Detaching
Done Detaching Detaching ret = 0x0
ProfilerDetachSucceeded
Shutdown 
Destroying CorProfilerInstance

As far as I can find, 0x80131363 is CORPROF_E_UNSUPPORTED_CALL_SEQUENCE as from here which I totally have no clue how that could happen and be solved. :(

bigbearzhu avatar Nov 16 '16 04:11 bigbearzhu

I landed here because I have the same issue when attempting to force GC on w3wp

MatteoPiovanelli-Laser avatar Oct 13 '20 13:10 MatteoPiovanelli-Laser

I have same issue found on a different process (unrelated to w3wp). Mine is a very thread-hungry process, in case that rings some bells.

msfcolombo avatar Dec 07 '22 22:12 msfcolombo