Talkredius
My application :

I'm using openPLC on Paspi 3 as a Modbus RTU to Modbus TCP Gateway
running the blank program

My modbusmaster.cpp has some modifications :

all RTU calls are divided into chunks of 20 registers per call
so that I can read any number of registers

Holding registers can be read via function code 4

Modbus TCP connection :

I have two apps polling data from openplc  via Modbus TCP :

app1 is polling 24/7, therefor the PLC log message says :
(Reg3000 is the RTU device)

Device Stromzaehler.Reg3000 is disconnected. Attempting to reconnect...
Setting main thread priority to RT
Locking main thread memory
Server: Listening on port 502
Server: waiting for new client...
Connected to MB device Stromzaehler.Reg3000
Server: Client accepted! Creating thread for the new client ID: 5...
Getting current time
Server: waiting for new client...
Server: Thread created for client ID: 5


If I start only app1 openplc works without any flaws about  3 month or so.

app2 creates and opens a TCP socket, reads the data and then closes the socket for one data poll
and does the same for the next data poll
When I start app2 I see a lot of these messages until openplc stops working :


.......

Server: Thread created for client ID: 6
Server: Client accepted! Creating thread for the new client ID: 7...
Server: waiting for new client...
Server: Thread created for client ID: 7
....


After starting app2 memory usage of openplc increases dramatically until it stops working :


  PID USER  PR NI VIRT  RES    SHR  S %CPU %MEM TIME+ COMMAND
2369 root -31 0 194320 191896 2896 S 1.7 20.2 0:20.32 openplc
  792 root  20 0  76236  20324 5828 S 0.0 2.1 0:27.33 nodejs


What I did so far for debugging :

in  /var/log/messages I found this :

"kthreadd invoked oom-killer: gfp_mask=0x26000c0, order=1, oom_score_adj=0"

Looks is if the Raspi runs out of memory, my prime suspect : openplc

Next steps :

Restarting openplc via remote web interface
looking for the PID of nodejs and openplc :

ps ax|fgrep nodejs
ps ax|fgrep openplc

Start top with these PIDs

top -p2200,792

When I start only app1 memory usage of openplc stays at 2,9 %
When I start app2 memory usage increases until openplc is stopped

My conclusion so far :

Looks as if openplc doesn't close and free the memory of a socket when the client closes this socket
I will dig deeper.



 

Talkredius
ok, finally I found the bug in core/server.cpp :

Here my changes + explanation :

changes in core/server.cpp in function startServer :

else
{
int arguments[1];
pthread_t thread;
int ret = -1;

printf("Server: Client accepted! Creating thread for the new client ID: %d...\n", client_fd);
arguments[0] = client_fd;
ret = pthread_create(&thread, NULL, handleConnections, arguments);
if (ret==0) {
pthread_detach(thread);
}
}


A thread created with pthread_create doesn't free all of its used resources when it is finished. Only a joined or detached thread does this.
Joining ihmo doesn't make sense here, so I choose detach.

changes in core/server.cpp in function handleConnections :


if (messageSize <= 0 || messageSize > 1024)
{
// something has gone wrong or the client has closed connection
if (messageSize == 0)
{
printf("Server: client ID: %d has closed the connection\n", client_fd);
}
else
{
printf("Server: Something is wrong with the client ID: %d message Size : %i\n", client_fd, mess$
}
break;
}

processMessage(buffer, messageSize, client_fd);
}
printf("Debug: Closing client socket and calling pthread_exit in server.cpp\n");
close(client_fd);
pthread_exit(NULL);


close(client_fd) will release all resources used by socket client_fd
pthread_exit isn't needed but I think it is a way of documenting that we are in a thread and leaving it.
Reply 0 0
thiagoralves
Wow! Good job. Thanks for the fix. Would you like to create a git pull request so that you can have the credits for your fix?
Reply 0 0
Talkredius
thanks for the flowers.
I'm not so familiar yet with git pull things etc. and I'm quite occupied by my profession in the next 2 weeks.
+ I'm too old to hunt for credits [smile]
+ summer is coming => have to get my PV + EV charging automation up and running [smile]
+ actually the credit has to go you, using this open source software saves me a lot of time

So I attached the server,cpp file.

Will attach my modfied  modbus_master.ccp asap this week too.
Reply 0 0
thiagoralves
Thank you for the file. I'll update OpenPLC code with your modifications.
Reply 0 0
Talkredius
some additional notes :

with my changes I ran into problems when using nodejs, it crashes after 2..3 days.
kernel log files says oom-killer has killed nodejs.

Also apps2, a php-program polling data via modbus tcp every 10 seconds,  throws execptions round about 4 in a hour, saying that already a data request is in progress.

while running openplc as stand alone without nodejs both apps are running for > 14 days, 24/7 without any problems, no thrown exceptions ,
no performance issues, no memory increase..

My guess, this crashes might caused by the debug messages and performance issues with nodejs and/or server.js
At the moment I can't dig deeper.
Reply 0 0
thiagoralves
I will take a look at that. NodeJS is just spawning a new process and redirecting its output. It shouldn't be causing any trouble.
Reply 0 0
Talkredius
Talkredius wrote:
ok, finally I found the bug in core/server.cpp :

Here my changes + explanation :

changes in core/server.cpp in function startServer :

Code:
 
else
{
int arguments[1];
pthread_t thread;
int ret = -1;

printf("Server: Client accepted! Creating thread for the new client ID: %d...\n", client_fd);
arguments[0] = client_fd;
ret = pthread_create(&thread, NULL, handleConnections, arguments);
if (ret==0) {
pthread_detach(thread);
}
}




close(client_fd) will release all resources used by socket client_fd
pthread_exit isn't needed but I think it is a way of documenting that we are in a thread and leaving it.


After some time I noticed on one Raspi lots of sockets in CLOSE_WAIT state on the Raspi
After some debugging my assumption is a race condition between pthread_detach and the exit of startServer

If startServer is finished before pthread_detach is completed the detached thread is never executed and the corresponding socket never closed.
Solved this by putting usleep(5000); after pthread_detach.

Perhaps a problem with the pthread library ?

On a second Raspi with the same hardware, same nearly the software I didn't notice any sockets in CLOSE_WAIT state, no idea why this happens only on this one Raspi.
Reply 0 0
Reply