Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Remove printing if Decref is called with NULL. Rename Decref/Incref to XDecref/XIncref#275

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

ArvidJB
Copy link
Contributor

self.repr is set in tp_repr() when called (which also increases the reference count). But if it's not populated we should not decrease the reference count

Copy link
Member

@filmorfilmor left a comment

Choose a reason for hiding this comment

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

TheRuntime.Decref andIncref check for the pointer being 0 in all cases. Did you see any actual problems with this part of the code?

@den-run-ai
Copy link
Contributor

@filmor actuallyDecref only it printsDecref(NULL) ifIntPtr.Zero. We have a lot of cases whereIntPtr.Zero is checked beforeDecref. But I agree that use case here is missing.

Also there are 2 places where Runtime.Decref(self.repr) is called in the same file.

@ArvidJB
Copy link
ContributorAuthor

ArvidJB commentedOct 19, 2016
edited
Loading

The check was the problem, actually: it calls
DebugUtil.Print("Decref(NULL)");
which tries to print to the Console, which sometimes throws an

Exception thrown: 'System.IO.IOException' in mscorlib.dll
Additional information: The handle is invalid.

then (for some reason I don't fully understand)

@ArvidJB
Copy link
ContributorAuthor

I found the reason for the IOException: it is due to this:

Console class members that work normally when the underlying stream is directed to a console might throw an exception if the stream is redirected, for example, to a file. Program your application to catch System.IO.IOException exceptions if you redirect a standard stream. You can also use the IsOutputRedirected, IsInputRedirected, and IsErrorRedirected properties to determine whether a standard stream is redirected before performing an operation that throws an System.IO.IOException exception.

fromhttps://msdn.microsoft.com/en-us/library/system.console.aspx?f=255&MSPPError=-2147217396

Even without those problems the spurious "Decref("NULL")" error messages are pretty annoying.

Any objections to this change?

@den-run-ai
Copy link
Contributor

I agree with this change but why not change in both places?

Runtime.Decref(self.repr);

Runtime.Decref(self.repr);

On Thu, Oct 20, 2016 at 8:53 AM, ArvidJBnotifications@github.com wrote:

I found the reason for the IOException: it is due to this:

Console class members that work normally when the underlying stream is
directed to a console might throw an exception if the stream is redirected,
for example, to a file. Program your application to catch
System.IO.IOException exceptions if you redirect a standard stream. You can
also use the IsOutputRedirected, IsInputRedirected, and IsErrorRedirected
properties to determine whether a standard stream is redirected before
performing an operation that throws an System.IO.IOException exception.

fromhttps://msdn.microsoft.com/en-us/library/system.console.
aspx?f=255&MSPPError=-2147217396

Even without those problems the spurious "Decref("NULL")" error messages
are pretty annoying.

Any objections to this change?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#275 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AHgZ5cU3bBt2jEc_2hxZTctxidgHIrIoks5q13JsgaJpZM4KbPz8
.

@filmor
Copy link
Member

filmor commentedOct 20, 2016
edited
Loading

Well, isn't the right thing to do then fixing/removing the debug log and documenting thatIncref andDecref are safe to call onnull?

EDIT: Just to clarify, even if the code does not go into the home-made decref (https://github.com/pythonnet/pythonnet/blob/master/src/runtime/runtime.cs#L567), it callsPy_DecRef which according to thePython docs is an exported version ofPy_XDECREF which does the extraNULL check. There's no point in doing that twice nor log about that.

@den-run-ai
Copy link
Contributor

den-run-ai commentedOct 20, 2016
edited
Loading

Is it really safe to callDecref onIntPtr.Zero? Why does pythonnet
have these ~9 checks all over the code?

Find all "if.*\!\=.*IntPtr.Zero.*\n.*(\n.*){0,7}Runtime.Decref", Regularexpressions, Find Results 1, Entire Solution, ""  C:\Python\pythonnet\pythonnet\src\runtime\classbase.cs(238): if (dict != IntPtr.Zero)  C:\Python\pythonnet\pythonnet\src\runtime\classderived.cs(850):             if (dict != IntPtr.Zero)  C:\Python\pythonnet\pythonnet\src\runtime\converter.cs(297):           if (p != IntPtr.Zero)  C:\Python\pythonnet\pythonnet\src\runtime\converter.cs(307):       if ((c != IntPtr.Zero) && (c != value))  C:\Python\pythonnet\pythonnet\src\runtime\exceptions.cs(196):       if (op != IntPtr.Zero)  C:\Python\pythonnet\pythonnet\src\runtime\managedtype.cs(51):       if ((e != IntPtr.Zero) && (e != ob))  C:\Python\pythonnet\pythonnet\src\runtime\metatype.cs(177):            if(init != IntPtr.Zero)  C:\Python\pythonnet\pythonnet\src\runtime\methodbinder.cs(336):                     if (pyoptype != IntPtr.Zero)  C:\Python\pythonnet\pythonnet\src\runtime\methodbinding.cs(164):                   if (baseMethod != IntPtr.Zero)  C:\Python\pythonnet\pythonnet\src\runtime\pythonexception.cs(155):             if (_pyTB != IntPtr.Zero)  Matching lines: 10    Matching files: 9    Total files searched: 99

On Thu, Oct 20, 2016 at 9:16 AM, Benedikt Reinartz <notifications@github.com

wrote:

Well, isn't the right thing to do then fixing/removing the debug log and
documenting that Incref and Decref are safe to call on null?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#275 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AHgZ5R5HLnFLNwwXb6ODQlW2uWWEZ5NLks5q13fZgaJpZM4KbPz8
.

@ArvidJB
Copy link
ContributorAuthor

@denfromufa i made the other change as well. I missed it since it looks like we were not hitting that problem in ConstructorBinding.tp_dealloc in our codebase.
@filmor I think this is actually a useful message since it basically tells you that somewhere you did some inconsistent reference counting.

@filmor
Copy link
Member

@denfromufa I don't know why those checks are in, but just look at the function, both implementations (withPy_DEBUG set and without) check explicitly for the pointer being 0.
@ArvidJB Not really. If our reference counting is actually off in an unsafe way we would never notice, as pointers are not set to 0 when they are freed byDecref.

I vote we remove the message instead and document the behaviour.

@den-run-ai
Copy link
Contributor

I agree that DebugUtil.Print() is a useful method and it should probably be
aSEPARATE PR to improve it for cases when console output is redirected.

So I agree with this pull request now, but generally we have a lot of other
places where ref count is not checked for null before decrementing.

On Thu, Oct 20, 2016 at 10:57 AM, ArvidJBnotifications@github.com wrote:

@denfromufahttps://github.com/denfromufa i made the other change as
well. I missed it since it looks like we were not hitting that problem in
ConstructorBinding.tp_dealloc in our codebase.
@filmorhttps://github.com/filmor I think this is actually a useful
message since it basically tells you that somewhere you did some
inconsistent reference counting.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#275 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AHgZ5ebgxZPkqND7fIGbsCMXiGfE3v9Tks5q149lgaJpZM4KbPz8
.

@ArvidJB
Copy link
ContributorAuthor

@denfromufa sounds good to me.
I will try to see how easy it is to fix the general problem of writing to the Console. Maybe it's as easy as writing to stdout directly?

@den-run-ai
Copy link
Contributor

@filmor do you mean that this line below is the check for the pointer being zero?

if((void*)0!=p)

Where is equivalent check for Py_DEBUG version? I did not find this!

@ArvidJB somehow my previous message that I sent using gmail was pushed/updated only 9 hours ago on github. But in my gmail I sent it 5 days ago, before@filmor 's last message. This is just to clarify the sequence of messages we had 5 days ago.

@tonyroberts@vmuriart do you guys have any better suggestions for this pull request?

In my opinion, we need to address 2 questions:

  1. Is it safe to Decref without checking for IntPtr.Zero? Hopefully someone can write a test for this, if this is safe. And then all ~9 manual checks that I referenced above have to be removed from pythonnet codebase.
  2. Do we need a debug message when trying to decref IntPtr.Zero? If yes, how to handle it safely for redirected outputs?

@tonyroberts
Copy link
Contributor

Just saw filmor's earlier comment that the exported Py_DecRef function is actually Py_XDECREF.

What I would do is rename Decref to Py_XDECREF and keep the null check and remove the logging - then it's clear to everyone that it can be called safely with a null pointer.

@den-run-ai
Copy link
Contributor

@tonyroberts thanks for pointing to@filmor 's edit above. Indeed, I did not notice it either. Especially, since I was looking at my email. So new messages about edits are generally better + optional in-place edits.

@ArvidJB now I completely agree with the latest comments from@filmor and@tonyroberts. Let me know if anything is not clear? Are you planning to continue with this pull request?

@ArvidJB
Copy link
ContributorAuthor

@denfromufa That makes sense, I will try to make that change. Should I open a new pull request for that or is it okay to continue on this one?

@den-run-ai
Copy link
Contributor

We can continue on this pull request and from your branch. Once ready, we
can just squash your commits into "logical unit(s)".

On Wed, Oct 26, 2016 at 11:12 AM, ArvidJBnotifications@github.com wrote:

@denfromufahttps://github.com/denfromufa That makes sense, I will try
to make that change. Should I open a new pull request for that or is it
okay to continue on this one?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#275 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AHgZ5Uv7pg30Y99r8zc-J_tGtdhfjA__ks5q33wBgaJpZM4KbPz8
.

abessen added2 commitsOctober 26, 2016 23:50
…heck for NULLRemove NULL check debug print in DecrefRemove added NULL checks for self.repr in constructorbinding.cs
@ArvidJB
Copy link
ContributorAuthor

I renamed Incref/Decref to XIncref/XDecref instead of Py_XINCREF/Py_XDECREF since that felt like a more C# appropriate name. I removed the additional NULL check I had added and also removed the code that prints on NULL in Decref.

tonyroberts and den-run-ai reacted with thumbs up emoji

@den-run-ai
Copy link
Contributor

looks like@tonyroberts approved your commit@ArvidJB

@filmor do you agree with these changes?

@filmor
Copy link
Member

I agree, but please rename and squash the PR before merging.

@ArvidJBArvidJB changed the titleDo not call Decref if self.repr is still IntPtr.ZeroRemove printing if Decref is called with NULL. Rename Decref/Incref to XDecref/XIncrefOct 28, 2016
@den-run-ai
Copy link
Contributor

@filmor now we can squash or rebase and merge pull requests with one button click (select a drop-down on merge pull request). More info in this article:

https://github.com/blog/2141-squash-your-commits

@den-run-aiden-run-ai merged commit86937bb intopythonnet:masterOct 29, 2016
@ArvidJBArvidJB deleted the constructorbinding_repr_refcount branchOctober 31, 2016 02:47
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@filmorfilmorfilmor left review comments

@den-run-aiden-run-aiden-run-ai approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@ArvidJB@den-run-ai@filmor@tonyroberts

[8]ページ先頭

©2009-2025 Movatter.jp