Timer in app stalls while evaluating function

This is in Matlab 2022a. The function the timer calls sends a udp message and updates a plot. I'm hoping somebody recognizes this error because I'm having trouble telling where it's getting stuck. The timer stalls somewhere in this function (pollBoardA), and if I hit ctrl-c it breaks out and prints the following message. I can then restart the timer and everything works again. I have not found a reliable way to make this happen but it's happening pretty often. I suspect it's related to the plotting but haven't confirmed it yet since the problem is intermittent.
Operation terminated by user during pollBoardA
In timer/timercb (line 114)
feval(val{1}, obj, eventStruct, val{2:end});
In matlab.ui.internal.componentframework.services.optional.EventDispatcherAddOn>@()obj.ProxyView.sendEventToClient('flush',pvPairs) (line 77)
func = @() obj.ProxyView.sendEventToClient('flush', pvPairs);
In matlab.ui.internal.dialog.DialogHelper.dispatchWhenPeerNodeViewIsReady (line 276)
func()
In matlab.ui.internal.componentframework.services.optional.EventDispatcherAddOn/sendFlushEventToClient (line 79)
matlab.ui.internal.dialog.DialogHelper.dispatchWhenPeerNodeViewIsReady(model, obj.ProxyView, func, obj);

7 Comments

@Jane Sprigg - is pollBoardA your callback? Can you show us the code?
This is basically what's in pollboardA which the timer calls. Outside of the timer are udp callbacks which read and parse the data as it arrives and stores it in bufferin. The timer keeps getting stuck somewhere in this script I think. The timer stops updating and Matlab shows as busy and when I hit ctrl-c the error (above) sounds like it's stuck in pollboardA. I tried disabling the plot updates and it still happened.
function pollBoardA(obj, event, app)
drawnow
% wI=in udp port, receive messages
% wO=out udp port, send messages
z=app.wO2;
params=app.params;
ip=params.ip;
port2=params.portout2;
if app.emulMSG
if app.MSG1CheckBox.Value
write(z,uint8(app.MSG1),'uint8',ip,port2);
end
if app.MSG2CheckBox.Value
write(z,uint8(app.MSG2),'uint8',ip,port2);
end
end
%app.bufferin is updated in a different script when the udpcallbacks read a
%message
xSize=length(app.bufferin.m1(:,1));
%%
if app.MSG1CheckBox.Value
if strcmp(app.TabGroup2.SelectedTab.Title,'Messages')
if strcmp(app.TabGroup3.SelectedTab.Title,'MSG1')
set(app.ImHandles.R1,'XData',1:xSize,'YData',app.bufferin.m1(:,app.ImHandles.R1col))
end
end
end
if app.MSG2.Value
if strcmp(app.TabGroup2.SelectedTab.Title,'Messages')
if strcmp(app.TabGroup3.SelectedTab.Title,'MSG2')
set(app.ImHandles.R4,'XData',1:xSize,'YData',app.bufferin.m4(:,app.ImHandles.R4col))
end
end
end
app.t2.InstantPeriod
@Jane Sprigg - how often does the timer fire? Since the issue doesn't seem to be with updating the plot, could the problem be with the write?
The timer fires every 33ms, roughly. The messages also come into a udp callback at about that rate.
I think the problem is with the write (or the read), but that's a matlab write to the udpport. Not shown in the function is a udpcallback (to a different port) which reads the port and updates the buffer variable.
It still hangs when I used the mex tcpip/udp to write, so I kind of think it's the udp callback stalling somehow. Even though it's not used in this function at all.
I was having trouble in this part in Matlab 2021a too, except instead of stalling Matlab was crashing (access violation warnings and just shutting down). I contacted Mathworks and they said I needed to install updates. I can't install updates so I upgraded to 2022a and now I'm having even more problems. Or different problems.
@Jane Sprigg - the 33 ms rate may be too fast. Have you considered lowering this rate so that there are fewer requests for data?
I can't lower the rate. Is there a limit in matlab on how fast I can request data?
I'm not sure what (or if) there is a limit on how fast you can request data. If you could lower the rate, it would be interesting to see if the problem still occurs.

Sign in to comment.

Answers (1)

Hello Jane,
I understand that you want to resolve the error message which indicates that the MATLAB operation was terminated due to a user interrupt (Ctrl+C) during the execution of the “pollBoardA” function.
Based on the provided information, it looks like is called by a timer callback. When you press Ctrl+C, MATLAB throws an error and displays the stack trace of where it was in the execution process when the interruption occurred.
The stack trace points towards an issue related to the timer callback and the flushing of events to the client, which is likely related to the graphics rendering or the GUI update.
Here are some steps you can take to troubleshoot and potentially resolve the issue:
  1. Isolate the Problem: Try to isolate the problem by temporarily removing the plotting code from the ‘pollBoardA’ function. If the timer no longer stalls, it suggests that the plotting is indeed related to the issue. You've mentioned that disabling plot updates did not resolve the issue, so the next step is to look at the UDP communication.
  2. Check UDP Operations: Ensure that UDP operations are not blocking the MATLAB thread. If the write operations to the UDP port are waiting for some event that never occurs, they could hang the function. You could add some debug prints before and after the UDP write calls to see if they are completing successfully.
  3. Use Asynchronous Plot Updates: The ‘drawnow’ function forces MATLAB to perform all pending graphical updates, which can be a blocking operation. Consider using ‘drawnow limitrate’ or ‘drawnow update’ to perform quicker, less resource-intensive updates.
  4. Debugging: Use MATLAB's debugging tools to set breakpoints and step through the ‘pollBoardA’ function to see exactly where it might be getting stuck.
  5. Error Handling: Implement error handling within your ‘pollBoardA’ function to catch any issues that arise during its execution. This can help prevent the timer from stalling.
  6. Check for Resource Leaks: Check if system resources are being exhausted. For example, if the buffer ‘app.bufferin’ grows indefinitely, it could be consuming too much memory, leading to performance issues.
  7. Check for Infinite Loops or Deadlocks: Ensure that there are no infinite loops or deadlocks within the callback or any related code, especially if there are dependencies between the timer callback and other parts of your application.
For more information on related queries, I recommend the following resources:
  1. App timer slows when mousing over plot: https://www.mathworks.com/support/search.html/answers/1814705-app-timer-slows-when-mousing-over-plot.html?fq%5B%5D=asset_type_name:answer&fq%5B%5D=category:matlab/desktop-tools-and-development-environment&page=1
  2. Ending a function after a time duration: https://www.mathworks.com/support/search.html/answers/291401-ending-a-function-after-a-time-duration.html?fq%5B%5D=asset_type_name:answer&fq%5B%5D=category:matlab/control-flow&page=1
  3. Appdesigner GUI issue code: https://www.mathworks.com/support/search.html/answers/1614760-appdesigner-gui-issue-code.html?fq%5B%5D=asset_type_name:answer&fq%5B%5D=category:matlab/app-designer&page=1
Hope this information is helpful to you!

Products

Release

R2022a

Asked:

on 11 Aug 2022

Answered:

on 28 Dec 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!