Recent

Author Topic: SimpleIPC on Windows  (Read 1741 times)

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
SimpleIPC on Windows
« on: December 15, 2019, 10:57:47 am »
I am using SimpleIPC in my project, what works fine on Linux and Mac hangs at exit on Windows.  Here is the simplest code to reproduce my problem, this in an otherwise plain new application -

Code: Pascal  [Select][+][-]
  1. uses simpleipc;
  2. var  CommsServer : TSimpleIPCServer;
  3.  
  4. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  5. begin
  6.     CommsServer.stopserver;
  7.     CommsServer.Free;
  8. end;
  9.  
  10. procedure TForm1.FormShow(Sender: TObject);
  11. begin
  12.     CommsServer  := TSimpleIPCServer.Create(Nil);
  13.     CommsServer.ServerID:='my-sipc-test';
  14.     CommsServer.Global:=True;
  15.     CommsServer.StartServer(true);             // start listening, threaded
  16. end;

If I change the StartServer(true) to startServer(false) it works fine, even on windows. That switch seems to run the watching process in a separate thread, I clearly don't understand the implication of that !

If I try to debug, I loose the debugger as soon as I step into or over the CommsServer.stopserver; line.

Any suggestions anyone, is it because I am some how not doing thread compatible things (even though they work fine on Mac and Linux) ?

Davo



Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: SimpleIPC on Windows
« Reply #1 on: December 15, 2019, 11:05:41 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormShow(Sender: TObject);
Seriously? Is that on the OnShow event? Really?

Well, that is one quite big error, there may be more.

(if you don't see it: OnShow/OnHide can happen multiple times. OnClose just once. It also means that the code is wrong for all platforms and sheer luck that it works on some)
« Last Edit: December 15, 2019, 11:11:37 am by Thaddy »
Specialize a type, not a var.

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: SimpleIPC on Windows
« Reply #2 on: December 15, 2019, 11:18:14 am »
I did say its a simple example of the problem.  Nothing to do with the problem.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: SimpleIPC on Windows
« Reply #3 on: December 15, 2019, 11:32:05 am »
I did say its a simple example of the problem.  Nothing to do with the problem.

Davo
Then at least provide a correct example, so I can rely on some knowledge and do not have to focus on basic mistakes.
Specialize a type, not a var.

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: SimpleIPC on Windows
« Reply #4 on: December 15, 2019, 11:44:44 am »
I certainly would not want you to waste your time on me Thaddy.

I am sure someone else can help.  Or I'll just set my app to not use the threaded version on Windows. So, relax, I'd rather you were not worried in any way !

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: SimpleIPC on Windows
« Reply #5 on: December 15, 2019, 01:22:53 pm »
You know you will get a proper answer when you explain it better.
Specialize a type, not a var.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: SimpleIPC on Windows
« Reply #6 on: December 15, 2019, 04:29:10 pm »
I certainly would not want you to waste your time on me Thaddy.

I am sure someone else can help.  Or I'll just set my app to not use the threaded version on Windows. So, relax, I'd rather you were not worried in any way !

Davo

Ignore him, it must be that time of the month!  :D
The only true wisdom is knowing you know nothing

PaulRowntree

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
Re: SimpleIPC on Windows
« Reply #7 on: December 15, 2019, 06:08:14 pm »
Try using the OnCreate and OnDestroy event handlers to create and free the key objects of the program.  They are called only once each, so you only create and free once.  No loose ends possible.plus, i think the FormClose handler expects you to provide feedback in the CanClose parameter; not sure what the default behaviour is if you don't provide that information.  The OD is asking for permission to close the window, and while you are deleting something, you should also confirm your intentions to the OS.
 
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: SimpleIPC on Windows
« Reply #8 on: December 16, 2019, 12:03:09 am »
Sorry, I have created confusion with what I posted. I always try to solve a problem myself so have tried lots of combinations before the particular combination in that block of source.  The last variant was the one I copied and pasted. The original had the SimpleIPC created in OnCreate and freed in OnDestroy.

The thing that stops the problem is turning off threading in the SimpleIPC server, its absolutely not about where the server is created or freed.

It also does not hang if the free-ing happens in the same function as create. Not useful but demonstrates something ?  What ?  To be absolutely clear, this does not work either -

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   CommsServer  := TSimpleIPCServer.Create(Nil);
  4.   CommsServer.ServerID:='my-sipc-test';
  5.   CommsServer.Global:=True;
  6.   CommsServer.StartServer(true);             // start listening, threaded
  7. end;
  8.  
  9. procedure TForm1.FormDestroy(Sender: TObject);
  10. begin
  11.   CommsServer.stopserver;
  12.   CommsServer.Free;
  13. end;
   

Set threading off { CommsServer.StartServer(false); }  and its all good. Appears the threading is incompatible with the Lazarus GUI loop ?

(Note - Linux requires you to define USECTHREADS in the lpr file.)

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: SimpleIPC on Windows
« Reply #9 on: December 16, 2019, 12:30:25 am »
I don't know who wrote the IPC server/client code, but I think you should file a bug report so that the issue might have a chance to get fixed (it won't here). Add the code of your previous post as a project to demonstrate the issue.

No, I checked fpc trunk and it seems that the issue has been fixed already, the fix will also be contained in v3.2 (whenever it it released...).
« Last Edit: December 16, 2019, 12:40:08 am by wp »

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: SimpleIPC on Windows
« Reply #10 on: December 16, 2019, 03:26:16 am »
Yep, looks like several possibly related fixes in FPC 3.2.

Thanks WP, I will run unthreaded for now, will add it to my list of good things that can happen when 3.2 is released (sigh).

I just needed to be reassured I was not the cause of the problem !

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018