question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Some of the functions seem to leak memory. The test program below will continuously eat up more and more memory.

I suspect the issue is in your use of Py_BuildValue without freeing the char* afterwards. For example, in curvemath_mul, resultX and resultY are allocated but never freed.

#!/usr/bin/env python

import fastecdsa.curvemath, fastecdsa.curve, gc

sx = str(fastecdsa.curve.secp256k1.G[0])
sy = str(fastecdsa.curve.secp256k1.G[1])
sz = str(2**255-1)

while True:
    fastecdsa.curvemath.mul(sx, sy, sz, 'secp256k1')
    gc.collect()

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
EggPoolcommented, Sep 4, 2019

I gave this a try with current version and secp256k1: signature checks do not leak anymore, but signing does.

import gc, os, psutil
from hashlib import sha1

from fastecdsa import ecdsa
from fastecdsa.curve import secp256k1
from fastecdsa.keys import gen_keypair

message = b"Sample message for signature testing"
priv, pub = gen_keypair(secp256k1)

CHECK_SIGN = True
CHECK_VERIFY = False

process = psutil.Process(os.getpid())
r, s = ecdsa.sign(message, priv, hashfunc=sha1, curve=secp256k1)

while True:
    print(process.memory_info().rss)
    if CHECK_SIGN:
        for i in range(1000):
            r, s = ecdsa.sign(message, priv, hashfunc=sha1, curve=secp256k1)
        gc.collect()
    if CHECK_VERIFY:
        for i in range(1000):
            check_sig = ecdsa.verify((r, s), message, pub, curve=secp256k1, hashfunc=sha1)
        gc.collect()
1reaction
targoncommented, Feb 8, 2017

Attached patch fixes a number of memory leaks. After applying it, I can run curvemath.mul, curvemath.add and _ecdsa.verify in loops without increasing the memory usage.

memleak.patch.txt

Can you please check & merge?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Memory leak - Wikipedia
In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in...
Read more >
What is Memory Leak? How can we avoid? - GeeksforGeeks
Memory leak occurs when programmers create a memory in heap and forget to delete it. The consequences of memory leak is that it...
Read more >
Definition of memory leak - PCMag
When memory is allocated, but not deallocated, a memory leak occurs (the memory has leaked out of the computer). If too many memory...
Read more >
Memory Leaks and Garbage Collection | Computerworld
DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a...
Read more >
Find a memory leak - Windows drivers - Microsoft Learn
A memory leak occurs when a process allocates memory from the paged or nonpaged pools, but doesn't free the memory.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found