Skip to content

Commit 1b37ec0

Browse files
committed
pybricks.tools: Fix race=False in multitask().
Fix wrong index direction for fetching the race keyword argument in pb_type_Task_new(). n_args - 1 would be the last positional argument, n_args would be the race keyword itself and n_args + 1 is the value of the race keyword argument, which is what we want. Also add a test to verify that it is working as expected.
1 parent 30e40ad commit 1b37ec0

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
- Make 3D orientation default for heading and drivebase heading control ([support#1962]).
99

1010
### Fixed
11+
- Fixed `race=False` ignored in `pybricks.tools.multitask()`. ([support#2468])
1112
- Fixed Essential Hub button being disabled after power off so it could not be
1213
switched on again.
1314

1415
[support#1962]: https://github.com/pybricks/support/issues/1962
16+
[support#2468]: https://github.com/pybricks/support/issues/2468
1517

1618
## [4.0.0b2] - 2025-11-25
1719

@@ -22,7 +24,7 @@
2224

2325
### Added
2426
- Experimental support for USB connectivity on SPIKE Prime ([pybricks-micropython#208]). Currently disabled by default.
25-
- Initial support for `pybricks.iodevices.UARTDevice` ([support#220]).
27+
- Initial support for `pybricks.iodevices.UARTDevice` ([support#220]).
2628
- Enabled previously hidden support for multiple code
2729
slots ([pybricks-micropython#264], [pybricks-micropython#312]).
2830
- Added the following status flags to the Pybricks protocol:
@@ -185,7 +187,7 @@
185187
- Fixed battery charging timeout if it didn't reach 100% after about 6 hours.
186188
This is same behavior observed in official firmware. ([pybricks-micropython#292]).
187189
- Fixed inconsistent battery level reported to user. Now it uses the same value
188-
as used by the light indicator ([support#2055]).
190+
as used by the light indicator ([support#2055]).
189191
- Fixed `DriveBase.angle()` getting an incorrectly rounded gyro value, which
190192
could cause `turn(360)` to be off by a degree ([support#1844]).
191193
- Fixed `hub` silently ignoring non-orthogonal base axis when it should raise.

pybricks/tools/pb_type_task.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ MP_DEFINE_CONST_DICT(pb_type_Task_locals_dict, pb_type_Task_locals_dict_table);
138138
static mp_obj_t pb_type_Task_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
139139

140140
// Whether to race until one task is done (True) or wait for all tasks (False).
141-
bool race = n_kw == 1 && mp_obj_is_true(args[n_args - 1]);
141+
bool race = n_kw == 1 && mp_obj_is_true(args[n_args + 1]);
142142

143143
pb_type_Task_obj_t *self = mp_obj_malloc(pb_type_Task_obj_t, type);
144144
self->num_tasks = n_args;

tests/virtualhub/multitasking/basic.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,40 @@ async def test_race_2():
163163
print(type(e), e.args)
164164

165165

166+
async def test_race_false():
167+
print("test_race_false")
168+
169+
task1_cancel = CallCounter()
170+
task2_cancel = CallCounter()
171+
172+
task1 = return_after(1, task1_cancel)
173+
task2 = return_after(2, task2_cancel)
174+
175+
# returns [0, 1] - both allowed to complete
176+
print(await multitask(task1, task2, race=False))
177+
178+
# neither should be canceled
179+
print(task1_cancel.count, task2_cancel.count)
180+
181+
# both should raise StopIteration because they are done
182+
183+
try:
184+
next(task1)
185+
except StopIteration as e:
186+
print(type(e), e.args)
187+
188+
try:
189+
next(task2)
190+
except StopIteration as e:
191+
print(type(e), e.args)
192+
193+
166194
async def main():
167195
await test_all_1()
168196
await test_all_2()
169197
await test_race_1()
170198
await test_race_2()
199+
await test_race_false()
171200

172201

173202
run_task(main())

tests/virtualhub/multitasking/basic.py.exp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ test_race_2
1818
1 0
1919
<class 'StopIteration'> ()
2020
<class 'StopIteration'> ()
21+
test_race_false
22+
(0, 1)
23+
0 0
24+
<class 'StopIteration'> ()
25+
<class 'StopIteration'> ()

0 commit comments

Comments
 (0)