Robot Code Crashes on cancelAll()

Our robot code will crash whenever we call cancelAll(). I can get the error code but we call this for testing because we have different commands in different modes and we don’t want anything to break when we re-enable. I can send the error code later. By the way we are using the CommandScheduler (new command based stuff) and it crashes on the CommandScheduler.getInstance().cancelAll() call

What does the stacktrace have in it?

Error at java.base/java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:719): Unhandled exception: java.util.ConcurrentModificationException
        at java.base/java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:719)
        at java.base/java.util.LinkedHashMap$LinkedKeyIterator.next(LinkedHashMap.java:741)
        at edu.wpi.first.wpilibj2.command.CommandScheduler.cancelAll(CommandScheduler.java:378)
        at frc.robot.Robot.autonomousInit(Robot.java:78)
        at edu.wpi.first.wpilibj.IterativeRobotBase.loopFunc(IterativeRobotBase.java:220)
        at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:82)
        at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:276)
        at edu.wpi.first.wpilibj.RobotBase.lambda$startRobot$0(RobotBase.java:329)
        at java.base/java.lang.Thread.run(Thread.java:834)

Looks like a bug in the API.

Their code:

public void cancelAll() { for (Command command : m_scheduledCommands.keySet()) { cancel(command); } }
The iterator.get method is structurally modifying the set.

I think that m_scheduledCommands.values() is what they should be iterating over to avoid it.

The structure of the m_scheduledCommands is a Map<Command, CommandState>. So the .keySet() is correct to get the Command. Should I just wait for an update?

1 Like

Fixed in this PR. Will be in the next release.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.