View Issue Details

IDProjectCategoryView StatusLast Update
0038959LazarusIDEpublic2021-06-03 19:14
ReporterMartin Friebe Assigned ToMattias Gaertner  
PrioritynormalSeverityminorReproducibilitysometimes
Status resolvedResolutionfixed 
PlatformLinuxOSFedora 
Product Version2.1 (SVN) 
Summary0038959: Pontential dead lock in ExtTool
DescriptionBelow is the valgrind (helgrind) output. This is what happens

ExtTools.pas line 380
in procedure TExternalTool.AddOutputLines(Lines: TStringList);

  EnterCriticalSection;
  try
  ....
    if OldMsgCount<WorkerMessages.Count then begin
      for i:=0 to ViewCount-1 do begin
      ....
        Views[i].ProcessNewMessages(Thread);
      end;
    end;
  finally
    LeaveCriticalSection;
  end;

TExternalTool is locked BEFORE the view

=====
ideexterntoolintf.pas line 2325

destructor TExtToolView.Destroy;
begin
  // wait for other threads to finish their access
  EnterCriticalSection;
  try
    if (Tool<>nil) and (not (csDestroying in Tool.ComponentState)) then
      Tool.RemoveView(Self);
    RemoveAsyncOnChanged;
    ClearLines;
    FreeAndNil(FProgressLine);
    FreeAndNil(FPendingLines);
    FreeAndNil(FPendingProgressLine);
    inherited Destroy;
  finally
    LeaveCriticalSection;
  end;
  FreeAndNil(FLines);
end;

The View enters the lock first,
then in "Tool.RemoveView(Self);" the tool can get destroyed, and the tool enters the lock

If 2 locks are entered in different order, then they can deadlock.
It is not clear if that can happen in this instance....

I.e. if the 2 locks are only ever both locked by the main thread / and if any other thread only ever locks only the one OR the other, but never both => then it is ok.
(Though the potential remains that locks are later added in threads, and it wont be ok any longer)


Steps To Reproduce
==82251==
==82251== ----------------------------------------------------------------
==82251==
==82251== Thread 0000001: lock order "0x19089328 before 0x6113B18" violated
==82251==
==82251== Observed (incorrect) order is: acquisition of lock at 0x6113B18
==82251== at 0x483EB42: mutex_lock_WRK (hg_intercepts.c:918)
==82251== by 0x4842A3A: pthread_mutex_lock (hg_intercepts.c:934)
==82251== by 0x4509F2: CTHREADS_$$_CENTERCRITICALSECTION$formal (cthreads.pp:558)
==82251== by 0x440172: SYSTEM_$$_ENTERCRITICALSECTION$TRTLCRITICALSECTION (thread.inc:235)
==82251== by 0xA4CAD3: IDEEXTERNTOOLINTF$_$TMESSAGELINES_$__$$_ENTERCRITICALSECTION (ideexterntoolintf.pas:1705)
==82251== by 0xA504F3: IDEEXTERNTOOLINTF$_$TEXTTOOLVIEW_$__$$_ENTERCRITICALSECTION (ideexterntoolintf.pas:2472)
==82251== by 0xA4FAA3: IDEEXTERNTOOLINTF$_$TEXTTOOLVIEW_$__$$_DESTROY (ideexterntoolintf.pas:2327)
==82251== by 0xD1056F: ETMESSAGEFRAME$_$TLMSGWNDVIEW_$__$$_DESTROY (etmessageframe.pas:780)
==82251== by 0x437117: SYSTEM$_$TOBJECT_$__$$_FREE (objpas.inc:336)
==82251== by 0xD18191: ETMESSAGEFRAME$_$TMESSAGESCTRL_$__$$_CLEARVIEWS$BOOLEAN (etmessageframe.pas:2583)
==82251== by 0xD1EFDE: ETMESSAGEFRAME$_$TMESSAGESFRAME_$__$$_CLEARVIEWS$BOOLEAN (etmessageframe.pas:3648)
==82251== by 0xD1FE59: ETMESSAGESWND$_$TMESSAGESVIEW_$__$$_CLEAR (etmessageswnd.pas:188)
==82251==
==82251== followed by a later acquisition of lock at 0x19089328
==82251== at 0x483EB42: mutex_lock_WRK (hg_intercepts.c:918)
==82251== by 0x4842A3A: pthread_mutex_lock (hg_intercepts.c:934)
==82251== by 0x4509F2: CTHREADS_$$_CENTERCRITICALSECTION$formal (cthreads.pp:558)
==82251== by 0x440172: SYSTEM_$$_ENTERCRITICALSECTION$TRTLCRITICALSECTION (thread.inc:235)
==82251== by 0xA4CAD3: IDEEXTERNTOOLINTF$_$TMESSAGELINES_$__$$_ENTERCRITICALSECTION (ideexterntoolintf.pas:1705)
==82251== by 0xA4A1A6: IDEEXTERNTOOLINTF$_$TABSTRACTEXTERNALTOOL_$__$$_ENTERCRITICALSECTION (ideexterntoolintf.pas:1228)
==82251== by 0xAEB926: EXTTOOLS$_$TEXTERNALTOOL_$__$$_DESTROY (exttools.pas:521)
==82251== by 0xAF1E4E: EXTTOOLSIDE$_$TEXTERNALTOOLIDE_$__$$_DESTROY (exttoolside.pas:55)
==82251== by 0x437117: SYSTEM$_$TOBJECT_$__$$_FREE (objpas.inc:336)
==82251== by 0xA4A3A1: IDEEXTERNTOOLINTF$_$TABSTRACTEXTERNALTOOL_$__$$_AUTOFREE (ideexterntoolintf.pas:1255)
==82251== by 0xA4B430: IDEEXTERNTOOLINTF$_$TABSTRACTEXTERNALTOOL_$__$$_REMOVEVIEW$TEXTTOOLVIEW (ideexterntoolintf.pas:1446)
==82251== by 0xA4FB02: IDEEXTERNTOOLINTF$_$TEXTTOOLVIEW_$__$$_DESTROY (ideexterntoolintf.pas:2330)
==82251==
==82251== Required order was established by acquisition of lock at 0x19089328
==82251== at 0x483EB42: mutex_lock_WRK (hg_intercepts.c:918)
==82251== by 0x4842A3A: pthread_mutex_lock (hg_intercepts.c:934)
==82251== by 0x4509F2: CTHREADS_$$_CENTERCRITICALSECTION$formal (cthreads.pp:558)
==82251== by 0x440172: SYSTEM_$$_ENTERCRITICALSECTION$TRTLCRITICALSECTION (thread.inc:235)
==82251== by 0xA4CAD3: IDEEXTERNTOOLINTF$_$TMESSAGELINES_$__$$_ENTERCRITICALSECTION (ideexterntoolintf.pas:1705)
==82251== by 0xA4A1A6: IDEEXTERNTOOLINTF$_$TABSTRACTEXTERNALTOOL_$__$$_ENTERCRITICALSECTION (ideexterntoolintf.pas:1228)
==82251== by 0xAEAEF5: EXTTOOLS$_$TEXTERNALTOOL_$__$$_ADDOUTPUTLINES$TSTRINGLIST (exttools.pas:380)
==82251== by 0xAF0CAB: EXTTOOLS$_$TEXTERNALTOOLTHREAD_$__$$_EXECUTE (exttools.pas:1629)
==82251== by 0x5608BF: CLASSES_$$_THREADFUNC$POINTER$$INT64 (tthread.inc:94)
==82251== by 0x450544: CTHREADS_$$_THREADMAIN$POINTER$$POINTER (cthreads.pp:311)
==82251== by 0x4841737: mythread_wrapper (hg_intercepts.c:398)
==82251== by 0x486E3F8: start_thread (in /usr/lib64/libpthread-2.32.so)
==82251==
==82251== followed by a later acquisition of lock at 0x6113B18
==82251== at 0x483EB42: mutex_lock_WRK (hg_intercepts.c:918)
==82251== by 0x4842A3A: pthread_mutex_lock (hg_intercepts.c:934)
==82251== by 0x4509F2: CTHREADS_$$_CENTERCRITICALSECTION$formal (cthreads.pp:558)
==82251== by 0x440172: SYSTEM_$$_ENTERCRITICALSECTION$TRTLCRITICALSECTION (thread.inc:235)
==82251== by 0xA4CAD3: IDEEXTERNTOOLINTF$_$TMESSAGELINES_$__$$_ENTERCRITICALSECTION (ideexterntoolintf.pas:1705)
==82251== by 0xA504F3: IDEEXTERNTOOLINTF$_$TEXTTOOLVIEW_$__$$_ENTERCRITICALSECTION (ideexterntoolintf.pas:2472)
==82251== by 0xA4FC2E: IDEEXTERNTOOLINTF$_$TEXTTOOLVIEW_$__$$_PROCESSNEWMESSAGES$TTHREAD (ideexterntoolintf.pas:2356)
==82251== by 0xAEB07E: EXTTOOLS$_$TEXTERNALTOOL_$__$$_ADDOUTPUTLINES$TSTRINGLIST (exttools.pas:399)
==82251== by 0xAF0CAB: EXTTOOLS$_$TEXTERNALTOOLTHREAD_$__$$_EXECUTE (exttools.pas:1629)
==82251== by 0x5608BF: CLASSES_$$_THREADFUNC$POINTER$$INT64 (tthread.inc:94)
==82251== by 0x450544: CTHREADS_$$_THREADMAIN$POINTER$$POINTER (cthreads.pp:311)
==82251== by 0x4841737: mythread_wrapper (hg_intercepts.c:398)
==82251==
==82251== Lock at 0x19089328 was first observed
==82251== at 0x484299B: pthread_mutex_init (hg_intercepts.c:796)
==82251== by 0x4509A1: CTHREADS_$$_CINITCRITICALSECTION$formal (cthreads.pp:544)
==82251== by 0x440132: SYSTEM_$$_INITCRITICALSECTION$TRTLCRITICALSECTION (thread.inc:223)
==82251== by 0xA4C850: IDEEXTERNTOOLINTF$_$TMESSAGELINES_$__$$_CREATE$TOBJECT$TMESSAGELINECLASS$$TMESSAGELINES (ideexterntoolintf.pas:1679)
==82251== by 0xA49E72: IDEEXTERNTOOLINTF$_$TABSTRACTEXTERNALTOOL_$__$$_CREATE$TCOMPONENT$$TABSTRACTEXTERNALTOOL (ideexterntoolintf.pas:1193)
==82251== by 0xAEB72D: EXTTOOLS$_$TEXTERNALTOOL_$__$$_CREATE$TCOMPONENT$$TEXTERNALTOOL (exttools.pas:507)
==82251== by 0xAF1CEB: EXTTOOLSIDE$_$TEXTERNALTOOLIDE_$__$$_CREATE$TCOMPONENT$$TEXTERNALTOOLIDE (exttoolside.pas:49)
==82251== by 0xAEFDAE: EXTTOOLS$_$TEXTERNALTOOLS_$__$$_ADD$ANSISTRING$$TABSTRACTEXTERNALTOOL (exttools.pas:1280)
==82251== by 0x1340E88: COMPILER$_$TCOMPILER_$__$$_COMPILE$crc0BFF8617 (compiler.pp:337)
==82251== by 0x4E1C36: MAIN$_$TMAINIDE_$__$$_DOBUILDPROJECT$TCOMPILEREASON$TPROJECTBUILDFLAGS$BOOLEAN$$TMODALRESULT (main.pp:7042)
==82251== by 0x4E31DE: MAIN$_$TMAINIDE_$__$$_DOINITPROJECTRUN$$TMODALRESULT (main.pp:7284)
==82251== by 0x4E3590: MAIN$_$TMAINIDE_$__$$_DORUNPROJECT$$TMODALRESULT (main.pp:7321)
==82251== Address 0x19089328 is 24 bytes inside a block of size 200 alloc'd
==82251== at 0x483B8A9: malloc (vg_replace_malloc.c:307)
==82251== by 0x44E568: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==82251== by 0x43DB69: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==82251== by 0x43731F: SYSTEM$_$TOBJECT_$__$$_NEWINSTANCE$$TOBJECT (objpas.inc:437)
==82251== by 0xA4C7F4: IDEEXTERNTOOLINTF$_$TMESSAGELINES_$__$$_CREATE$TOBJECT$TMESSAGELINECLASS$$TMESSAGELINES (ideexterntoolintf.pas:1677)
==82251== by 0xA49E72: IDEEXTERNTOOLINTF$_$TABSTRACTEXTERNALTOOL_$__$$_CREATE$TCOMPONENT$$TABSTRACTEXTERNALTOOL (ideexterntoolintf.pas:1193)
==82251== by 0xAEB72D: EXTTOOLS$_$TEXTERNALTOOL_$__$$_CREATE$TCOMPONENT$$TEXTERNALTOOL (exttools.pas:507)
==82251== by 0xAF1CEB: EXTTOOLSIDE$_$TEXTERNALTOOLIDE_$__$$_CREATE$TCOMPONENT$$TEXTERNALTOOLIDE (exttoolside.pas:49)
==82251== by 0xAEFDAE: EXTTOOLS$_$TEXTERNALTOOLS_$__$$_ADD$ANSISTRING$$TABSTRACTEXTERNALTOOL (exttools.pas:1280)
==82251== by 0x1340E88: COMPILER$_$TCOMPILER_$__$$_COMPILE$crc0BFF8617 (compiler.pp:337)
==82251== by 0x4E1C36: MAIN$_$TMAINIDE_$__$$_DOBUILDPROJECT$TCOMPILEREASON$TPROJECTBUILDFLAGS$BOOLEAN$$TMODALRESULT (main.pp:7042)
==82251== by 0x4E31DE: MAIN$_$TMAINIDE_$__$$_DOINITPROJECTRUN$$TMODALRESULT (main.pp:7284)
==82251== Block was alloc'd by thread 0000001
==82251==
==82251== Lock at 0x6113B18 was first observed
==82251== at 0x484299B: pthread_mutex_init (hg_intercepts.c:796)
==82251== by 0x4509A1: CTHREADS_$$_CINITCRITICALSECTION$formal (cthreads.pp:544)
==82251== by 0x440132: SYSTEM_$$_INITCRITICALSECTION$TRTLCRITICALSECTION (thread.inc:223)
==82251== by 0xA4C850: IDEEXTERNTOOLINTF$_$TMESSAGELINES_$__$$_CREATE$TOBJECT$TMESSAGELINECLASS$$TMESSAGELINES (ideexterntoolintf.pas:1679)
==82251== by 0xA4F776: IDEEXTERNTOOLINTF$_$TEXTTOOLVIEW_$__$$_CREATELINES (ideexterntoolintf.pas:2307)
==82251== by 0xA4F956: IDEEXTERNTOOLINTF$_$TEXTTOOLVIEW_$__$$_CREATE$TCOMPONENT$$TEXTTOOLVIEW (ideexterntoolintf.pas:2318)
==82251== by 0xD103BF: ETMESSAGEFRAME$_$TLMSGWNDVIEW_$__$$_CREATE$TCOMPONENT$$TLMSGWNDVIEW (etmessageframe.pas:772)
==82251== by 0xD18441: ETMESSAGEFRAME$_$TMESSAGESCTRL_$__$$_GETVIEW$ANSISTRING$BOOLEAN$$TLMSGWNDVIEW (etmessageframe.pas:2616)
==82251== by 0xD1EEE2: ETMESSAGEFRAME$_$TMESSAGESFRAME_$__$$_GETVIEW$ANSISTRING$BOOLEAN$$TLMSGWNDVIEW (etmessageframe.pas:3628)
==82251== by 0xD1FF82: ETMESSAGESWND$_$TMESSAGESVIEW_$__$$_GETVIEW$ANSISTRING$BOOLEAN$$TEXTTOOLVIEW (etmessageswnd.pas:210)
==82251== by 0xD202B9: ETMESSAGESWND$_$TMESSAGESVIEW_$_CREATEVIEW$ANSISTRING$$TEXTTOOLVIEW_$$_TRYCAPTION$crcDA660A49 (etmessageswnd.pas:218)
==82251== by 0xD2003A: ETMESSAGESWND$_$TMESSAGESVIEW_$__$$_CREATEVIEW$ANSISTRING$$TEXTTOOLVIEW (etmessageswnd.pas:226)
==82251== Address 0x6113b18 is 24 bytes inside a block of size 200 alloc'd
==82251== at 0x483B8A9: malloc (vg_replace_malloc.c:307)
==82251== by 0x44E568: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==82251== by 0x43DB69: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==82251== by 0x43731F: SYSTEM$_$TOBJECT_$__$$_NEWINSTANCE$$TOBJECT (objpas.inc:437)
==82251== by 0xA4C7F4: IDEEXTERNTOOLINTF$_$TMESSAGELINES_$__$$_CREATE$TOBJECT$TMESSAGELINECLASS$$TMESSAGELINES (ideexterntoolintf.pas:1677)
==82251== by 0xA4F776: IDEEXTERNTOOLINTF$_$TEXTTOOLVIEW_$__$$_CREATELINES (ideexterntoolintf.pas:2307)
==82251== by 0xA4F956: IDEEXTERNTOOLINTF$_$TEXTTOOLVIEW_$__$$_CREATE$TCOMPONENT$$TEXTTOOLVIEW (ideexterntoolintf.pas:2318)
==82251== by 0xD103BF: ETMESSAGEFRAME$_$TLMSGWNDVIEW_$__$$_CREATE$TCOMPONENT$$TLMSGWNDVIEW (etmessageframe.pas:772)
==82251== by 0xD18441: ETMESSAGEFRAME$_$TMESSAGESCTRL_$__$$_GETVIEW$ANSISTRING$BOOLEAN$$TLMSGWNDVIEW (etmessageframe.pas:2616)
==82251== by 0xD1EEE2: ETMESSAGEFRAME$_$TMESSAGESFRAME_$__$$_GETVIEW$ANSISTRING$BOOLEAN$$TLMSGWNDVIEW (etmessageframe.pas:3628)
==82251== by 0xD1FF82: ETMESSAGESWND$_$TMESSAGESVIEW_$__$$_GETVIEW$ANSISTRING$BOOLEAN$$TEXTTOOLVIEW (etmessageswnd.pas:210)
==82251== by 0xD202B9: ETMESSAGESWND$_$TMESSAGESVIEW_$_CREATEVIEW$ANSISTRING$$TEXTTOOLVIEW_$$_TRYCAPTION$crcDA660A49 (etmessageswnd.pas:218)
==82251== Block was alloc'd by thread 0000001
==82251==
TagsNo tags attached.
Fixed in Revision65170.
LazTarget-
Widgetset
Attached Files

Activities

Mattias Gaertner

2021-06-03 19:14

manager   ~0131142

Tools, Views, etc are only created and freed by the main thread.

Issue History

Date Modified Username Field Change
2021-06-03 18:43 Martin Friebe New Issue
2021-06-03 19:14 Mattias Gaertner Assigned To => Mattias Gaertner
2021-06-03 19:14 Mattias Gaertner Status new => resolved
2021-06-03 19:14 Mattias Gaertner Resolution open => fixed
2021-06-03 19:14 Mattias Gaertner Fixed in Revision => 65170.
2021-06-03 19:14 Mattias Gaertner LazTarget => -
2021-06-03 19:14 Mattias Gaertner Note Added: 0131142