heap_lock heap_unlock not working

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
Polo
Posts: 10
Joined: Tue Oct 02, 2018 1:07 pm

heap_lock heap_unlock not working

Post by Polo » Tue Oct 02, 2018 1:23 pm

New account, so no code tags, sorry
When I run this code I get a MemmoryError: memory allocation failed, heap is locked

import micropython
micropython.alloc_emergency_exception_buf(100)
micropython.heap_lock()
micropython.heap_unlock()

Running this also locks the heap:
import micropython
micropython.heap_unlock()

My setup is: MicroPython v1.9.4 on 2018-05-11; PYBv1.1 with STM32F405RG

Is this a bug or am doing something wrong?
Thanks for any help

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

heap_lock heap_unlock not working

Post by jickster » Wed Oct 03, 2018 2:36 am

Which line causes the error?

What are you trying to do?


Sent from my iPhone using Tapatalk Pro

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: heap_lock heap_unlock not working

Post by stijn » Wed Oct 03, 2018 8:29 am

Reproducible on Windows builds, doesn't seem right indeed (so either it's a bug or it is not supposed to be used that way, didn't check the docs).

Code: Select all

>>> import micropython
>>> micropython.heap_lock()
>>> micropython.heap_unlock()
MemoryError: memory allocation failed, heap is locked
>>> (press Ctrl-D here)
FATAL: uncaught NLR 0F702020
and

Code: Select all

>>> import micropython
>>> micropython.heap_unlock()
>>> micropython.heap_unlock()
MemoryError: memory allocation failed, heap is locked
>>> (press Ctrl-D here)
FATAL: uncaught NLR 0F282020

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

Re: heap_lock heap_unlock not working

Post by pythoncoder » Wed Oct 03, 2018 8:38 am

I can replicate this with firmware built today. This is evidently a bug. I'll raise an issue.

[EDIT]
Now done issue 4205.
Peter Hinch
Index to my micropython libraries.

Polo
Posts: 10
Joined: Tue Oct 02, 2018 1:07 pm

Re: heap_lock heap_unlock not working

Post by Polo » Wed Oct 03, 2018 8:05 pm

The responses on the issue explain the behavior.
You can't use the console with the heap locked, and heap_unlock without a previous lock "fails".

Thanks everyone!

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

Re: heap_lock heap_unlock not working

Post by pythoncoder » Thu Oct 04, 2018 4:19 am

@Polo Your observation of a crash after two calls to heap_unlock did show up a bug. A PR with a fix has now been submitted.
Peter Hinch
Index to my micropython libraries.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: heap_lock heap_unlock not working

Post by jickster » Thu Oct 04, 2018 4:24 am

pythoncoder wrote:@Polo Your observation of a crash after two calls to heap_unlock did show up a bug. A PR with a fix has now been submitted.
What about if you call lock 65536 times?


Sent from my iPhone using Tapatalk Pro

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

Re: heap_lock heap_unlock not working

Post by pythoncoder » Thu Oct 04, 2018 4:32 am

jickster wrote:
Thu Oct 04, 2018 4:24 am
...
What about if you call lock 65536 times?...
You're doing an infinite recursion and are about to crash the stack ;)

I doubt this is remotely likely in a real application. Handling it properly (rather than just increasing the size of the counter) would be nontrivial. But if you feel inclined to submit a PR, I'll study it with relish ;)
Peter Hinch
Index to my micropython libraries.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: heap_lock heap_unlock not working

Post by jickster » Thu Oct 04, 2018 9:47 pm

pythoncoder wrote:
Thu Oct 04, 2018 4:32 am
jickster wrote:
Thu Oct 04, 2018 4:24 am
...
What about if you call lock 65536 times?...
You're doing an infinite recursion and are about to crash the stack ;)

I doubt this is remotely likely in a real application. Handling it properly (rather than just increasing the size of the counter) would be nontrivial. But if you feel inclined to submit a PR, I'll study it with relish ;)
Since we're handling underflow, we should definitely handle the overflow case.
There should be an exception if you have a lock-depth of more than 65535.

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

Re: heap_lock heap_unlock not working

Post by pythoncoder » Fri Oct 05, 2018 9:16 am

See discussion here issue 4205.
Peter Hinch
Index to my micropython libraries.

Post Reply