in reply to Re^7: Perl crash during perl_clone
in thread Perl crash during perl_clone
When your C/XS code receives the callback address from Perl, take a copy of the *current* context and save it away in a suitable place.
At this point, my script is just a single line of execution (no ithreads or anything else), except for the subs that are the callacks. So, can there still be more than one context when I get the callback address in XS?
With the "force context" solution, I encountered a crash (actually a "Not a CODE ref" assert) in a scenario when a 2nd callback was called from a 2nd thread. I was puzzled at that..but, perhaps perl.exe has more than one 'context' by default? Or perhaps its because I didnt "save" & "restore" contexts after the 1st callback that the context for the 2nd is messed up?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^9: Perl crash during perl_clone
by BrowserUk (Patriarch) on Oct 29, 2010 at 00:19 UTC | |
Maybe this will explain the save&restore and context caching mechanism better than I've achieved using words:
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
by perlmonk1729 (Acolyte) on Oct 29, 2010 at 09:28 UTC | |
However, my confusion was why there would be 'multiple' contexts: without any perl_clone, I expected only one perl context now. Anyhow, I modified my code to match the caching scheme and the results are same as when I 'forced' the context : the first callback is invoked fine, but the 2nd callback fails with "Undefined subroutine &main::0 called at" error. It points to a line that contains a "sleep()" statement in the main body of my perl-script. This however happens, just as the 2nd callback executes call_sv(). I also confirmed from prints that 'saved' context is set properly before invoking the call_sv. Unrelated, but the prints also show the same values for the 'saved' context in the two (actually 3) callbacks in my project. I am unable to run your code as my machine didnt have Inline installed & I'm now getting compile errors executing your code (_867652.xs:13: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before âCALLBACKâ etc). However, I'm posting the modified version to give a better picture of how my real project looks & hope it reproduces the error I'm seeing. I cant thank you enough for your efforts here!! ps: Ofcourse, in my real project is more involved (multiple callbacks executed in each thread & multiple such threads. callbacks and 'main' do lot more than sleep())
| [reply] [d/l] |
by BrowserUk (Patriarch) on Oct 29, 2010 at 11:57 UTC | |
Your code, with a couple of minor corrections: runs perfectly here! (Not bad for code you couldn't test :). So, where does that leave us? However, my confusion was why there would be 'multiple' contexts: without any perl_clone, I expected only one perl context now. If there is only one Perl thread, there will be only one Perl context. The problem is, when the callback is triggered and you enter the C-level callback routine (CCB) ready to call the Perl-level callback (PCB), Perl requires that context be established (set), *before* you call the PCB. But, as the CCB is just a C subroutine, that context is not established for you, as it would be with an XS routine. So, you have to do it yourself. Hence the need to hive it off somewhere, and establish it before calling back into Perl. "it" here being the global PerlInterpreter *saved(1|2) contexts. The local PerlInterpreter *temp contexts within the CCBs are actually redundant in my example code, because I know that the callbacks will only ever be initiated from the C-level timer threads. Ie. they will never be called from an existing Perl context. But, and it is a big one, you haven't clarified--nor from memory, even mentioned--what triggers the callbacks in your real application? And this could be very significant. For example, on my OS (windows), I know that asynchronous IO callbacks generally run in the context of a completely new thread, started & 'injected' into the Perl process, by the OS. As such, they are clean environments in which we can pretty much do what we like, because as soon as we return from them, they will just disappear with no lasting effects beyond what we program into them. However, if you are running on a *nix system, asynchronous events can be triggered through signals. Which means that the CCB will be invoked not in a new, clean thread, but rather in an existing (Perl created) thread, via a longjump. Which means that you need to be very careful what you do to both the global state, and thread-local state of perl during those CCBs. Hence the local save/restore of the temp perl context is my attempt to guess what might be necessary on your platform. (The errors you mention tell me I got it wrong!). And this is where my ability to help you curtails rapidly. Even if you can provide a clear and concise description of what triggers these callbacks, the chances are I will have little or no experience of those mechanisms on your platform. And even if you could put together some concise, stand-alone code that compiles on your platform and demonstrates the problems you are encountering, it almost certainly wouldn't compile and run here. Which again would leave me in the position of trying to run the code in my head--for an OS I have little knowledge of. The bottom line is that without a concise, easily buildable, runnable demonstration of the problem, there is little anyone can really do to help you. With such a demo, then you may well find a willing, thread-aware user on your platform (or one sufficiently similar) that can take up the baton. Either here, or perhaps the appropriate CPAN forum or mailing list (See the bottom of the threads POD). Such a demo might even draw a response from the most-likely-to-be-able-to-help people, those on p5p. But you need to make it easy for them to try your code and see the problem. I'm not abandoning you, just keeping it realistic about my chances of being able to help you. Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
by perlmonk1729 (Acolyte) on Oct 29, 2010 at 18:56 UTC | |
by BrowserUk (Patriarch) on Oct 30, 2010 at 13:12 UTC | |
| |