Skip to content

Code randomly does not invoke Magtag.exit_and_deep_sleep() #10639

Open

Description

@gglockner

CircuitPython version and board name

CircuitPython 10.0.0-beta.3, MagTag 2025 edition

Code/REPL

from adafruit_magtag.magtag import MagTag
from os import getenv
import gc

url = getenv("PWD_URL")
print("Getting data from URL "+url)
magtag = MagTag(
  url=url,
  json_path=[[], [], ["ssid"], ["passphrase"], ['timestamp'], ['qrcode']]
)

def update(magtag):
  try:
    result = magtag.fetch(auto_refresh=False)
    print("Retrieved data: "+str(result))
    magtag.graphics.qrcode(result[-1], qr_size=3, x=192, y=0)
    magtag.refresh()
    sleepTime = getenv("REFRESH_TIME")
  except Exception as e:
    print("Error-callback:\n", str(e))
    sleepTime = 60
  
  gc.collect()
  return sleepTime

title_font = getenv("TITLE_FONT")
header_font = getenv("HEADER_FONT")
body_font = getenv("BODY_FONT")
small_body_font = getenv("SMALL_BODY_FONT")

magtag.add_text(
    text_font=title_font,
    text_transform=lambda x: "Guest Wi-Fi",
)

magtag.add_text(
    text_font=header_font,
    text_position=(0,20),
    text_transform=lambda x: "Scan to connect",
)

magtag.add_text(
    text_font=body_font,
    text_position=(0,100),
    text_transform=lambda x: "Net:  "+x,
)

magtag.add_text(
    text_font=body_font,
    text_position=(0,115),
    text_transform=lambda x: "Pass: "+x,
)

magtag.add_text(
    text_font=small_body_font,
    text_position=(0,70),
    text_transform=lambda x: "Update: "+x,
)

sleepTime = update(magtag)
print("Entering deep sleep for "+str(sleepTime)+" sec")
magtag.exit_and_deep_sleep(sleepTime)

Behavior

Normally, when the code finishes, the console writes:

Code done running.

Press any key to enter the REPL. Use CTRL-D to reload.
Pretending to deep sleep until alarm, CTRL-C or file write.
Woken up by alarm.

However, after several hours, you see:

Code done running.

Press any key to enter the REPL. Use CTRL-D to reload.

and the board is frozen, without invoking deep sleep.

Description

No response

Additional information

No response

Activity

  1. dhalbert commented on Sep 22, 2025

    @dhalbert
    Collaborator

    This seems like it might be due to the host computer you are connected to going to sleep while "fake deep sleep" is happening. Is it true that host computer goes to sleep during the several hours you mentioned?

  2. added this to the Support milestone on Sep 22, 2025
  3. gglockner commented on Sep 22, 2025

    @gglockner
    Author

    @dhalbert : Thanks. I can reproduce this when the Magtag is disconnected from the computer. I just posted the console log for illustration.

    I am testing a much simpler version below. The board is currently disconnected from the computer. Will post an update in a few hours when I have tested it more.

    from adafruit_magtag.magtag import MagTag
    from os import getenv
    import terminalio
    
    url = getenv("PWD_URL")
    print("Getting data from URL "+url)
    magtag = MagTag(
      url=url,
      json_path=[['timestamp']]
    )
    
    magtag.add_text(
        text_font=terminalio.FONT,
        text_position=(20,20),
    )
    
    try:
      magtag.fetch()
    except Exception as e:
      print("Exception: "+str(e))
    
    sleepTime = 10
    
    print("Entering deep sleep for "+str(sleepTime)+" sec")
    magtag.exit_and_deep_sleep(sleepTime)
    
  4. dhalbert commented on Sep 22, 2025

    @dhalbert
    Collaborator

    If the magtag.fetch() fails, I think you will get an (uncaught) exception. So you could catch all exceptions and try again or just go to sleep.

  5. gglockner commented on Sep 22, 2025

    @gglockner
    Author

    Thanks but there is a try-except block in my main code already. (There wasn't in my attempt at a minimally reproducible sample - just fixed that above).

  6. gglockner commented on Sep 22, 2025

    @gglockner
    Author

    I can confirm that it fails with the simpler code above, even with the Magtag disconnected from a computer.

  7. gglockner commented on Sep 22, 2025

    @gglockner
    Author

    Here is a minimum reproducible example that fails to update after a few hours when the Magtag is plugged into a USB power supply, not a computer. Also, note that I have two Magtags and I can reproduce this failure on both boards, which suggests this is a software issue, not a hardware issue.

    from adafruit_magtag.magtag import MagTag
    import terminalio
    import random
    
    sleepTime = random.randint(1,30)
    
    magtag = MagTag()
    
    magtag.add_text(
        text_font=terminalio.FONT,
        text_position=(20,20),
        text="Sleeping for "+str(sleepTime)+" sec"
    )
    
    magtag.exit_and_deep_sleep(sleepTime)
    
  8. dhalbert commented on Sep 22, 2025

    @dhalbert
    Collaborator

    OK, so not even a fetch is involved. When you say "a few hours", is that time actually consistent?

  9. dhalbert commented on Sep 22, 2025

    @dhalbert
    Collaborator

    Could you try 9.2.9 as well? Thanks. It would be useful to know if this is a regression.

  10. gglockner commented on Sep 22, 2025

    @gglockner
    Author

    OK, so not even a fetch is involved. When you say "a few hours", is that time actually consistent?

    I don't know. It takes too long to babysit.

    Could you try 9.2.9 as well? Thanks. It would be useful to know if this is a regression.

    Unfortunately no, according to the instructions for installing CircuitPython:

    WARNING: The updated Adafruit MagTag 2025 Edition will not work with CircuitPython 9.2.x or earlier. Make sure you install 10.x.x or later!

    So I cannot run CircuitPython 9.2.9 for my MagTag 2025 Edition.

  11. dhalbert commented on Sep 22, 2025

    @dhalbert
    Collaborator

    Thanks anyway, I have an older MagTag and can test.

  12. gglockner commented on Sep 22, 2025

    @gglockner
    Author

    When you say "a few hours", is that time actually consistent?

    I modified the test so that it is deterministic. See below. I confirmed the number of iterations is not consistent: I got failures with 61 and 16 trials, on the same MagTag. To reset the counter, run the two commented lines via REPL.

    from adafruit_magtag.magtag import MagTag
    import terminalio
    import microcontroller
    
    ## INIT NVM via REPL
    # i = 0
    # microcontroller.nvm[0:8] = i.to_bytes(8)
    
    i = 1+int.from_bytes(microcontroller.nvm[0:8])
    
    magtag = MagTag()
    
    magtag.add_text(
        text_font=terminalio.FONT,
        text_position=(20,20),
        text="Trial "+str(i)
    )
    
    microcontroller.nvm[0:8] = i.to_bytes(8)
    
    magtag.exit_and_deep_sleep(2)
    
  13. 16 remaining items

  14. modified the milestones: 10.1.0, 10.x.x on Feb 7, 2026
  15. modified the milestones: 10.x.x, 10.3.0 on May 19, 2026
  16. dhalbert commented on Jul 24, 2026

    @dhalbert
    Collaborator

    @gglockner @bablokb I am looking at this again.

    @bablokb I get the idea from the above that adafruit/Adafruit_CircuitPython_MagTag#96 is sufficent for the MagTag display but not in general. So perhaps I should merge that library PR. @gglockner do you have any new opinions on this.

    @dhalbert: I am not sure what you mean here. Do you mean waiting for some "sufficient" time interval?

    @bablokb: Exactly: don't pass a busy_pin to make display.busy wait.

    @bablokb if, as you say, the option is only to wait, do you mean that for each display type we need to know how long it takes to finish its operations? Or that the CircuitPython e-ink display code can know when to stop waiting in general? Thanks.

  17. gglockner commented on Jul 24, 2026

    @gglockner
    Author

    Thanks @dhalbert, I have nothing new to add. In my own project, I simply implemented a workaround to loop while board.DISPLAY.busy is true.

  18. bablokb commented on Jul 24, 2026

    @bablokb

    That is also my solution. For the displays with a reliable busy-pin, I just wait on the pin. This also works with a 74HC165D. For other displays, I measure the typical time needed and just hardcode the wait-time.

    I don't think that deep-sleep should do that. This is the type of commodity thing that solves some problems while creating new ones. From Python-side, two lines are sufficient.

  19. gglockner commented on Jul 24, 2026

    @gglockner
    Author

    It is a common use case to update the e-Ink display immediately before putting the device into deep sleep. However, I believe that the status quo is deficient: I had to spend several days debugging what appeared as a random failure. Therefore I urge you to implement one of the following:

    1. Treat this as a code defect and update the CircuitPython libraries to delay deep sleep while board.DISPLAY.busy is true
    2. Treat this as a documentation issue that:
      a. Clearly documents that an update to the display may cause deep sleep to fail
      b. Includes a simple example showing a while loop that waits for board.DISPLAY.busy to be false before calling adafruit_magtag.magtag.MagTag.exit_and_deep_sleep()

    Thanks.

  20. modified the milestones: 10.3.0, 10.x.x on Aug 25, 2026
  21. modified the milestones: 10.x.x, 11.0.0 on Sep 28, 2026
  22. self-assigned this
    on Nov 20, 2026
  23. added a commit that references this issue on Nov 21, 2026
    4962dab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions