Skip to content

Commit f028bf5

Browse files
committed
remove concurrent avoiding modification member variables
exact same changes from wpilibsuite/allwpilib#6593
1 parent 4a953ab commit f028bf5

File tree

1 file changed

+7
-37
lines changed

1 file changed

+7
-37
lines changed

commands2/commandscheduler.py

+7-37
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,6 @@ def __init__(self) -> None:
100100
self._interruptActions: List[Callable[[Command, Optional[Command]], None]] = []
101101
self._finishActions: List[Callable[[Command], None]] = []
102102

103-
self._inRunLoop = False
104-
self._toSchedule: Dict[Command, None] = {}
105-
# python: toCancelInterruptors stored in _toCancel dict
106-
self._toCancel: Dict[Command, Optional[Command]] = {}
107-
# self._toCancelInterruptors: List[Optional[Command]] = []
108-
self._endingCommands: Set[Command] = set()
109-
110103
self._watchdog = Watchdog(TimedRobot.kDefaultPeriod, lambda: None)
111104

112105
hal.report(
@@ -187,10 +180,6 @@ def _schedule(self, command: Optional[Command]) -> None:
187180
reportWarning("Tried to schedule a null command!", True)
188181
return
189182

190-
if self._inRunLoop:
191-
self._toSchedule[command] = None
192-
return
193-
194183
self.requireNotComposed(command)
195184

196185
# Do nothing if the scheduler is disabled, the robot is disabled and the command
@@ -259,11 +248,15 @@ def run(self) -> None:
259248
loopCache.poll()
260249
self._watchdog.addEpoch("buttons.run()")
261250

262-
self._inRunLoop = True
263251
isDisabled = RobotState.isDisabled()
264252

265253
# Run scheduled commands, remove finished commands.
266254
for command in self._scheduledCommands.copy():
255+
if not self.isScheduled(command):
256+
# skip as the normal scheduledCommands was modified
257+
# and that command was canceled
258+
continue
259+
267260
if isDisabled and not command.runsWhenDisabled():
268261
self._cancel(command, None)
269262
continue
@@ -273,28 +266,14 @@ def run(self) -> None:
273266
action(command)
274267
self._watchdog.addEpoch(f"{command.getName()}.execute()")
275268
if command.isFinished():
276-
self._endingCommands.add(command)
269+
self._scheduledCommands.pop(command)
277270
command.end(False)
278271
for action in self._finishActions:
279272
action(command)
280-
self._endingCommands.remove(command)
281-
self._scheduledCommands.pop(command)
282273
for requirement in command.getRequirements():
283274
self._requirements.pop(requirement)
284275
self._watchdog.addEpoch(f"{command.getName()}.end(False)")
285276

286-
self._inRunLoop = False
287-
288-
# Schedule/cancel commands from queues populated during loop
289-
for command in self._toSchedule:
290-
self._schedule(command)
291-
292-
for command, interruptor in self._toCancel.items():
293-
self._cancel(command, interruptor)
294-
295-
self._toSchedule.clear()
296-
self._toCancel.clear()
297-
298277
# Add default commands for un-required registered subsystems.
299278
for subsystem, scommand in self._subsystems.items():
300279
if subsystem not in self._requirements and scommand is not None:
@@ -425,23 +404,14 @@ def _cancel(self, command: Command, interruptor: Optional[Command]):
425404
reportWarning("Tried to cancel a null command", True)
426405
return
427406

428-
if command in self._endingCommands:
429-
return
430-
431-
if self._inRunLoop:
432-
self._toCancel[command] = interruptor
433-
return
434-
435407
if not self.isScheduled(command):
436408
return
437409

438-
self._endingCommands.add(command)
410+
self._scheduledCommands.pop(command)
439411
command.end(True)
440412
for action in self._interruptActions:
441413
action(command, interruptor)
442414

443-
self._endingCommands.remove(command)
444-
self._scheduledCommands.pop(command)
445415
for requirement in command.getRequirements():
446416
del self._requirements[requirement]
447417
self._watchdog.addEpoch(f"{command.getName()}.end(true)")

0 commit comments

Comments
 (0)