Lazarus

Programming => LCL => Topic started by: ccrause on March 16, 2023, 06:38:44 am

Title: Desktop hangs when hitting break point in GTK2 TCombobox.OnDropDown
Post by: ccrause on March 16, 2023, 06:38:44 am
Hitting a breakpoint inside the OnDropDown event of a TComboBox in Linux with the LCL widget type set to GTK2 causes the desktop to hang. Functionality is restored when killing the Lazarus IDE from a separate terminal session.  This happen with both fpdebug and gdb debuggers. It does not happen when the widget set is QT5.

Any ideas why this happens and whether this is fixable?

To replicate, create a new application project and add a dropdown box to the form.  Add a handler for the OnDropDown event.  Place a breakpoint on the end of the handler.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     ComboBox1: TComboBox;
  13.     procedure ComboBox1DropDown(Sender: TObject);
  14.   private
  15.  
  16.   public
  17.  
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26.  
  27. procedure TForm1.ComboBox1DropDown(Sender: TObject);
  28. begin
  29.  
  30. end;
  31.  
  32. end.
  33.  

Tested on Lazarus 2.2.4 and 2.2.6 with FPC 3.2.2
Linux Mint 20.2 64 bit with MATE 1.24.0
Title: Re: Desktop hangs when hitting break point in GTK2 TCombobox.OnDropDown
Post by: dsiders on March 16, 2023, 07:48:49 am
Hitting a breakpoint inside the OnDropDown event of a TComboBox in Linux with the LCL widget type set to GTK2 causes the desktop to hang. Functionality is restored when killing the Lazarus IDE from a separate terminal session.  This happen with both fpdebug and gdb debuggers. It does not happen when the widget set is QT5.

Any ideas why this happens and whether this is fixable?

To replicate, create a new application project and add a dropdown box to the form.  Add a handler for the OnDropDown event.  Place a breakpoint on the end of the handler.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     ComboBox1: TComboBox;
  13.     procedure ComboBox1DropDown(Sender: TObject);
  14.   private
  15.  
  16.   public
  17.  
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26.  
  27. procedure TForm1.ComboBox1DropDown(Sender: TObject);
  28. begin
  29.  
  30. end;
  31.  
  32. end.
  33.  

Tested on Lazarus 2.2.4 and 2.2.6 with FPC 3.2.2
Linux Mint 20.2 64 bit with MATE 1.24.0

I'm  curious if it still happens when there is something in the event handler for the  breakpoint to act upon... instead of the end statement. Just a guess...
Title: Re: Desktop hangs when hitting break point in GTK2 TCombobox.OnDropDown
Post by: MarkMLl on March 16, 2023, 08:48:36 am
I'm  curious if it still happens when there is something in the event handler for the  breakpoint to act upon... instead of the end statement. Just a guess...

Generally speaking, yes. Breakpoints in event handlers are fraught... I've always assumed it was just "one of those things".

MarkMLl
Title: Re: Desktop hangs when hitting break point in GTK2 TCombobox.OnDropDown
Post by: Martin_fr on March 16, 2023, 08:57:27 am
Afaik it's an issue in gtk (or some versions/builds thereof).
The same should happen if you place "while true do ;" into your event handler.

Apparently there is only one event thread for all the apps in gtk. If one blocks, then all others block too.

I am unaware of a fix for it.
But there are still ways to debug.

You can't block the event handler. But the IDE has "debug history".
You can
- set a breakpoint
- go to the breakpoint properties (right click the breakpoint)
- remove the check/tick from "break"
- set the check for "take snapshot"

Run. Now your code will only pause for a less than a second on the breakpoint. It will then continue, therefore not block gtk.

Menu: View > Debug Windows > History

Each stop at the breakpoint added a history entry. (if there are more than 20 (or maybe 30), then select the camera (camera without plus) to see the full list).
Double click an entry to select.

The stack and watches window will show the value collected when the breakpoint was hit. So you can see what did happen.

Limits:
- It only collects watches (and locals) for the top stack frame.
- It only collects 5 stack frames in the stack window, unless the stack window is open, and set to show more.




Alternatively, remote debug. Then the desktop can freeze, but you need a 2nd machine (or run the to-be-debugged app in a virtual machine)
Title: Re: Desktop hangs when hitting break point in GTK2 TCombobox.OnDropDown
Post by: ccrause on March 16, 2023, 10:59:50 am
I'm  curious if it still happens when there is something in the event handler for the  breakpoint to act upon... instead of the end statement. Just a guess...

Generally speaking, yes. Breakpoints in event handlers are fraught... I've always assumed it was just "one of those things".

Indeed.  I stumbled into this problem when trying to debug code in the event handler. The minimum reproduceable example does not need any code, just the OnDropDown event handler.
Title: Re: Desktop hangs when hitting break point in GTK2 TCombobox.OnDropDown
Post by: ccrause on March 16, 2023, 12:36:15 pm
Afaik it's an issue in gtk (or some versions/builds thereof).
The same should happen if you place "while true do ;" into your event handler.

Apparently there is only one event thread for all the apps in gtk. If one blocks, then all others block too.

I am unaware of a fix for it.
Thank you Martin. I suspected the problem may be with GTK and not Lazarus but wasn't sure.

Quote
But there are still ways to debug.
Thank you for the debug tips.  In my case there was an issue with the code running in the event handler and it was easy to debug simply by moving the code outside of the event handler for testing.
Title: Re: Desktop hangs when hitting break point in GTK2 TCombobox.OnDropDown
Post by: MarkMLl on March 16, 2023, 06:42:15 pm
Alternatively, remote debug. Then the desktop can freeze, but you need a 2nd machine (or run the to-be-debugged app in a virtual machine)

That's something I've not tried, but I can't remember whether I've seen "breakpoint in event handler" crash the debugger or the entire desktop.

I /do/, on occasion, get desktop lockups while debugging: and this is likely to be a GTK2 program on a KDE desktop. Using a root console session to kill the program being debugged sorts that one.

Another possibility for debugging might be to have Lazarus etc. in a Docker session into which one can SSH. However I suspect that one would have to plan for that in advance by using -v to expose the gdbserver socket when the container was first created.

MarkMLl
TinyPortal © 2005-2018