The dreaded forgotten await

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

The dreaded forgotten await

Post by tve » Fri May 22, 2020 1:44 am

I don't know how many times over the past month I spend a good 10 minutes troubleshooting some mysterious failure to find that I had forgotten the "await" when calling a coro. Usually I'll look at that line of code multiple times and see nothing wrong with it before the lightbulb finally goes on. Is there any way to get an error message somehow somewhere so I don't spend half of my life chasing down this silly mistake over and over again?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: The dreaded forgotten await

Post by pythoncoder » Fri May 22, 2020 5:23 am

YES. I've suffered from that one too. It is infuriating.

However detecting a syntax error may not be easy as there are things you can legitimately do to a coroutine apart from awaiting it, such as assigning it to a variable or passing it to a function. I did look at writing a script to detect these silently failing errors, but there are a lot of edge cases. The following run without error on MicroPython and CPython 3.8 (changing the import line).

Code: Select all

import uasyncio as asyncio

async def foo():
    print('start')
    g = asyncio.sleep(2)
    await g
    print('done')

asyncio.run(foo())
Or how about

Code: Select all

import uasyncio as asyncio

async def bar(coro, t):
    print('start')
    await coro(t)
    print('done')


async def foo():
    await bar(asyncio.sleep, 2)

asyncio.run(foo())
Peter Hinch
Index to my micropython libraries.

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: The dreaded forgotten await

Post by tve » Fri May 22, 2020 7:20 am

I'm aware of the valid uses of coros without await... But at this point I'd be willing to have a checker that barfs unless there's an annotation of some form. In the end, having to add such annotations would be worth it if it saves the aggravations. At least to me.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: The dreaded forgotten await

Post by kevinkk525 » Fri May 22, 2020 7:23 am

In Cpython you would get a warning:

Code: Select all

Warning (from warnings module):
  File "<pyshell#8>", line 3
RuntimeWarning: coroutine 'test' was never awaited
But I have no idea how that could be implemented in micropython. It would be helpful.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply