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

Fix: allocate C memory for MDB_val in readonly Txn #29

Merged
merged 4 commits into from
Dec 7, 2023

Conversation

wojas
Copy link
Member

@wojas wojas commented Dec 7, 2023

Use C.malloc to allocate memory for MDB_val in readonly Txn.

CGo does not allow using new() for this. This can lead to an "cgo argument has Go pointer to unpinned Go pointer" CGo error.

Fixes #28.

TODO:

  • Also fix the FD() usage of new() if needed. Not needed, but changed syntax for clarity.

@fiatjaf: does this fix the error you encountered?

Use C.malloc to allocate memory for MDB_val in readonly Txn.
CGo does not allow using new() for this.

Fixes #28.
wojas added 2 commits December 7, 2023 20:45
Change the C.mdb_filehandle_t declaration to use var instead of new() to
clarify that we are not retaining a pointer to anything.

This is related to #28, but in this case no issue, because we are not
assiging a pointer to Go allocated memory to a C value.

Functionally this does not change anything. Go decides if values are
stack allocated or heap allocated based on if they escape, not based on
the syntax.
@wojas wojas changed the title WIP: Fix: allocate C memory for MDB_val in readonly Txn Fix: allocate C memory for MDB_val in readonly Txn Dec 7, 2023
lmdb/txn_test.go Outdated Show resolved Hide resolved
@@ -943,6 +943,9 @@ func TestTxn_Reset_readonly_C_free(t *testing.T) {

// Since this is a readonly transaction, the global Env key/val cannot be
// reused and new C memory must be allocated.
if txn.key == env.ckey || txn.val == env.cval {
t.Error("env.ckey and cval must not be used in a readonly Txn")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link

@shane-ns1 shane-ns1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@wojas wojas merged commit 28e8840 into PowerDNS:master Dec 7, 2023
4 checks passed
@wojas wojas added this to the v1.9.2 milestone Dec 7, 2023
@fiatjaf
Copy link

fiatjaf commented Dec 7, 2023

I've run my code with this patch for a while and didn't see the problem anymore. Thanks! Now I will also remove the goroutine stuff, which would probably have fixed it anyway somehow.

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

Successfully merging this pull request may close these issues.

Mysterious error: "cgo argument has Go pointer to unpinned Go pointer"
4 participants