Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leak in StrType::proxifyString #421

Open
philippedistributive opened this issue Aug 28, 2024 · 3 comments
Open

Leak in StrType::proxifyString #421

philippedistributive opened this issue Aug 28, 2024 · 3 comments
Assignees

Comments

@philippedistributive
Copy link
Collaborator

Issue type

Bug

How did you install PythonMonkey?

None

OS platform and distribution

No response

Python version (python --version)

No response

PythonMonkey version (pip show pythonmonkey)

No response

Bug Description

Get sanitizer leak report:

=================================================================
==39075==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 12352 byte(s) in 386 object(s) allocated from:
#0 0x7e75e44fc698 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
#1 0x7e75e01956d1 in StrType::proxifyString(JSContext*, JS::HandleJS::Value) /home/philippe/Sources/PythonMonkey/src/StrType.cc:114
#2 0x7e75e0196348 in StrType::getPyObject(JSContext*, JS::HandleJS::Value) /home/philippe/Sources/PythonMonkey/src/StrType.cc:194
#3 0x7e75e01b1f45 in pyTypeFactory(JSContext*, JS::HandleJS::Value) /home/philippe/Sources/PythonMonkey/src/pyTypeFactory.cc:56
#4 0x7e75e013a456 in getKey /home/philippe/Sources/PythonMonkey/src/JSObjectProxy.cc:124
#5 0x7e75e013af9d in JSObjectProxyMethodDefinitions::JSObjectProxy_get_subscript(JSObjectProxy*, _object*) /home/philippe/Sources/PythonMonkey/src/JSObjectProxy.cc:162
#6 0x7e75e3f45d0c in PyMapping_GetItemString Objects/abstract.c:2353

code sample:

#!/usr/bin/env python3
"""
Works at 1020000 elements, OOMs at 1030000 elements...
"""

import pythonmonkey as pm

oomer = pm.eval("""
function oomer()
{
let arr_size; // change array size to different values
arr_size = 1020000; // success!
arr_size = 1030000; // OOMs (ON MY SYSTEM! - maybe you'll have to up it to OOM on yours!)

const bigArray = [];
for (let i = 0; i < arr_size; i++)
bigArray.push(i + 0.1 / (i + 0.123456)); // randomish floats, fattened up by json.stringify A LOT later

//let seed = 1; bigArray.sort(() => (Math.sin(seed++) * 10000) % 1 - 0.5); // TODO unnecessary, remove later

// these initial values don't really matter per se, it's more just about how they're serialized
console.log(Array length: ${bigArray.length});
console.log(Array bytes : ${bigArray.length * 8}); // 8 bytes per js number???
console.log(Array MB : ${bigArray.length * 8 / 1000000});

// The following code is baed off of encodeJobValueList in dcp-client/job/index.js
const jsonedElementsArray = [];
for (let i = 0; i < bigArray.length; i++)
{
jsonedElementsArray.push(JSON.stringify(bigArray[i]));

// logging
if (i % 10000 === 0 && i > 600000) // skip first 600000 then only print every 10000 elements
  console.log(i, ' -- ', bigArray[i]);

}

// now we calculate the total length of all the strings in the array and see how much memory they use
console.log(JSONed Array length: ${jsonedElementsArray.length});
console.log(JSONed Array bytes : ${jsonedElementsArray.reduce((acc, str) => acc + str.length, 0) * 2}); // 2 bytes per character
console.log(JSONed Array MB : ${jsonedElementsArray.reduce((acc, str) => acc + str.length, 0) * 2 / 1000000});
}
oomer
""")

oomer()

Standalone code to reproduce the issue

No response

Relevant log output or backtrace

No response

Additional info if applicable

No response

What branch of PythonMonkey were you developing on? (If applicable)

No response

@Xmader
Copy link
Member

Xmader commented Aug 28, 2024

pyString->jsString = new JS::PersistentRootedValue(cx);

We have things like this almost everywhere 😞

@Xmader
Copy link
Member

Xmader commented Aug 28, 2024

I don't think the memory leak is related to the OOM issue though.

@philippedistributive
Copy link
Collaborator Author

philippedistributive commented Aug 28, 2024

@Xmader Yeah me neither, hence why it gets logged separately

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

3 participants