View Issue Details

IDProjectCategoryView StatusLast Update
0033130FPCFCLpublic2018-07-31 12:41
ReporterSergey Bulba Assigned ToMichael Van Canneyt  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
PlatformPCOSWindows 
Product Version3.0.4 
Summary0033130: simpleipc example not work in thread mode
DescriptionIn lazarus\fpc\3.0.4\source\packages\fcl-process\examples

if running ipc server in threaded mode

ipcserver.exe -t

and then run ipcclient.exe and client's SendStringMessage calling is not returning until breaking server or client by Ctrl-C in command line.
Steps To ReproduceJust compile lazarus\fpc\3.0.4\source\packages\fcl-process\examples\ipcclient.pp and lazarus\fpc\3.0.4\source\packages\fcl-process\examples\ipcserver.pp and run as described above.
Additional InformationMy old app for FPC 3.0.0 after recompiling in 3.0.4 is not raising ipcserver.onmessage (in 3.0.0 no threaded/non-threaded ipcserver switch yet), so I decided to test FPC 3.0.4 with native examples and found this bug.
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Relationships

related to 0031805 resolvedMichael Van Canneyt Simple IPC stopserver hangs app when threaded 

Activities

Sergey Bulba

2018-02-05 14:05

reporter   ~0106252

Seems TIPCServer.PeekMessage with False parameter is not thread save function after FPC 3.0.0

My 3.0.0 thread code was:
procedure TMessageHook.ApplyMessage;
begin
  IPCServer.PeekMessage(3, True)
end;

procedure TMessageHook.Execute;
begin
  while not Terminated do
  begin
   if IPCServer.Active then
    if IPCServer.PeekMessage(3, False) then
     Synchronize(@ApplyMessage);
  end;
end;

Just fixed it to:
procedure TMessageHook.Execute;
begin
  while not Terminated do
  begin
   if IPCServer.Active then
     Synchronize(@ApplyMessage);
  end;
end;

So, IPCServer.PeekMessage(3, False) is not thread-safe in 3.0.4.

If Thread function in SimpleIPCServer uses same code, this can be cause of deadlock in example project.

Sergey Bulba

2018-02-05 14:29

reporter   ~0106253

Its working, but so slow now.

So, need back thread-safe IPCServer.PeekMessage(TimeOut, False) or any other flag property, to call synchronize only if message is ready for reading.

Sergey Bulba

2018-02-05 14:52

reporter   ~0106254

Last edited: 2018-02-06 06:51

View 2 revisions

I found cause of slowdown, and finally used this thread code (idea can be used for simpleipc.pp patch):

type
 TMessageHook = class(TThread)
   procedure ApplyMessage;
 protected
   procedure Execute; override;
 end;

var
  MessageHook:TMessageHook;

procedure TMessageHook.ApplyMessage;
begin
  IPCServer.PeekMessage(0, True) // zero to return to TMessageHook thread as fast as possible
end;

procedure TMessageHook.Execute;
begin
  while not Terminated do
   begin
    if IPCServer.Active then
      Synchronize(@ApplyMessage);
    Sleep(3); //time delay inside of TMessageHook thread (not of main one)
   end;
end;

procedure StartIPC;
begin
IPCServer:=TSimpleIPCServer.Create(Nil);
IPCServer.ServerID:=IPCServerName;
IPCServer.Global:=True;
IPCServer.StartServer;
MessageHook := TMessageHook.Create(False);
end;

procedure StopIPC;
begin
  MessageHook.Terminate;
  MessageHook.WaitFor;
  MessageHook.Free;
  IPCServer.Free;
end;

Denis Kozlov

2018-02-06 04:02

reporter   ~0106260

Threaded mode is a bit broken in 3.0.X, but should be fixed in 3.1.1 (TRUNK).

More info here:
https://bugs.freepascal.org/view.php?id=31805

Michael Van Canneyt

2018-02-10 01:08

administrator   ~0106308

Can you please check with trunk, as Denis suggests, and see if the issue is fixed for you ?

Sergey Bulba

2018-02-16 01:12

reporter   ~0106398

Just tested 3.1.1 trunk, examples work fine in both modes (threaded and not threaded).

Michael Van Canneyt

2018-07-13 08:40

administrator   ~0109420

Reported as fixed in trunk by OP

Issue History

Date Modified Username Field Change
2018-02-05 12:49 Sergey Bulba New Issue
2018-02-05 14:05 Sergey Bulba Note Added: 0106252
2018-02-05 14:29 Sergey Bulba Note Added: 0106253
2018-02-05 14:52 Sergey Bulba Note Added: 0106254
2018-02-06 04:02 Denis Kozlov Note Added: 0106260
2018-02-06 06:51 Sergey Bulba Note Edited: 0106254 View Revisions
2018-02-10 01:07 Michael Van Canneyt Assigned To => Michael Van Canneyt
2018-02-10 01:07 Michael Van Canneyt Status new => assigned
2018-02-10 01:08 Michael Van Canneyt Relationship added related to 0031805
2018-02-10 01:08 Michael Van Canneyt Note Added: 0106308
2018-02-10 01:08 Michael Van Canneyt Status assigned => feedback
2018-02-16 01:12 Sergey Bulba Note Added: 0106398
2018-02-16 01:12 Sergey Bulba Status feedback => assigned
2018-07-13 08:40 Michael Van Canneyt Note Added: 0109420
2018-07-13 08:40 Michael Van Canneyt Status assigned => resolved
2018-07-13 08:40 Michael Van Canneyt Resolution open => fixed
2018-07-31 12:41 Sergey Bulba Status resolved => closed