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

Add reference count to LibGit2 objects #20124

Merged
merged 3 commits into from
Jan 21, 2017
Merged

Add reference count to LibGit2 objects #20124

merged 3 commits into from
Jan 21, 2017

Conversation

simonbyrne
Copy link
Contributor

Fixes segfault from calling git_libgit2_shutdown before finalizers (#20109).

Fixes segfault from calling `git_libgit2_shutdown` before finalizers (#20109).
@simonbyrne simonbyrne added the libgit2 The libgit2 library or the LibGit2 stdlib module label Jan 19, 2017
@simonbyrne simonbyrne requested a review from yuyichao January 19, 2017 10:01
@simonbyrne
Copy link
Contributor Author

I just realised that one alternative to this would be to call git_libgit2_init on each object initialization, and git_libgit2_shutdown at each finalizer, as these act like a reference counter.

@kshyatt
Copy link
Contributor

kshyatt commented Jan 20, 2017

Can this be reviewed/merged?

@tkelman
Copy link
Contributor

tkelman commented Jan 20, 2017

do we want the alternative, or this? pros/cons?

REFCOUNT[] -= 1
if REFCOUNT[] == 0
# will the last finalizer please turn out the lights?
ccall((:git_libgit2_shutdown, :libgit2), Cint, ())
Copy link
Member

Choose a reason for hiding this comment

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

What if the number of libgit2 objects drops to 0 at runtime. Is that ok?

Copy link
Contributor

Choose a reason for hiding this comment

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

The __init__ holds a global reference so the reference should not drop to 0.

@@ -9,6 +9,8 @@ export with, GitRepo, GitConfig
const GITHUB_REGEX =
r"^(?:git@|git://|https://(?:[\w\.\+\-]+@)?)github.com[:/](([^/].+)/(.+?))(?:\.git)?$"i

const REFCOUNT = Ref(zero(UInt))
Copy link
Contributor

Choose a reason for hiding this comment

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

I would set this to 0 in __init__ also.

Copy link
Contributor

Choose a reason for hiding this comment

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

s/0/1/g

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

@@ -603,8 +605,14 @@ function __init__()

err = ccall((:git_libgit2_init, :libgit2), Cint, ())
err > 0 || throw(ErrorException("error initializing LibGit2 module"))
REFCOUNT[] += 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally we'll need to replace these with atomic counters but this should be good enough for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed these to be atomic counters: can you have a quick look over it as I'm not really familiar with them.

@simonbyrne
Copy link
Contributor Author

simonbyrne commented Jan 20, 2017

do we want the alternative, or this? pros/cons?

The only differences I can tell:

  • the alternative seems to have some code for handling threading (I don't know how this would perform, though @yuyichao mentions atomic counter above)
    • now made the refcount atomic.
  • the alternative uses a Cint for the counter, so there is theoretically a potential for overflow on 64-bit machines.

@kshyatt
Copy link
Contributor

kshyatt commented Jan 21, 2017

Did the changes @yuyichao want get made? Can we merge this?

@kshyatt kshyatt merged commit d3bac6e into master Jan 21, 2017
@kshyatt kshyatt deleted the sb/libgit2/refcount branch January 21, 2017 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libgit2 The libgit2 library or the LibGit2 stdlib module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants