Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

call to Net::SSH2 functions crashes caller object

by jasathra (Initiate)
on Sep 20, 2013 at 08:39 UTC ( [id://1054979]=perlquestion: print w/replies, xml ) Need Help??

jasathra has asked for the wisdom of the Perl Monks concerning the following question:

I am using Perl Net::SSH2 for communication with a device. I have created a small package with a function that instantiate Net::SSH2 object, connects (via connect) to remote device, authenticate (auth_password), then creates a channel via (channel, blocking(1), process(shell), PRINT (command), READLINE, close (channel),) and then disconnect SSH2 connection”, and return response to calling module. I am using only the public methods provided by Perl Net::SSH2 module.

To have some generalization I am using this as a plugin via another module that uses Module::Pluggable. I tested the loading of the SSH2 plugin, and execution of commands via it in a sample script and it works fine.

I created a test Atm frm (using CPAN Test::Unit framework) which uses above connection module as follows; NetSSH2 plugin (exports run function, which does all the job of creating a connection and executing command) Module1 (plugins; NetSSH2, RPC, Telnet, Serial, …..), it first checks the plugin’s name, check if the plugin is available (if connection is successful…) and then returns its package name.

RunnerScript ( Contains Obj1),

Obj1 (creates and contains Obj2, Obj3),

Obj2 (calls Module1 to return desired plugin, and then holds the plugin package name), calls plugin run method directly as and when required.

Obj3 (calls plugin via Obj2)

Now when I use my NetSSH2 plugin in above structure I am facing following issues:

a. Ist command executed by Obj2/Obj3 via plugin’s run method succeed, but after that READLINE(one that reads channel for input data), while polling throws “Net::SSH2::poll: can't translate event 'in'”.On little debugging I found Net::SSH2 has a lexical variable (my %Event) that is populated when the package is loaded for the first time, it stores macros/call to Perl xtensions. During the first call the poll is able to access this %Event properly but later during subsequent calls this variable is corrupted/uninitialized and holds garbage values, so the above error. At times it has nothing (printing the hash via Dumper throws SEGV error).

I do not know why/how it retains value when I use the plugin via my sample script, so to circumvent this issue I change the declaration of the variable to ‘our’ and then exported it in the package. This let the variable holds the proper value during all the calls. With this my framework passed the first command invocation, but then I land in bigger issue ‘b’… Q: Is this a valid approach…?

b. After making the %Event global and exporting Obj2/Obj3 were able to make 2nd call, but then as soon as the function (run function in SSH2 plugin) returns, Obj2/Obj3 is corrupted, and I get SEGV error, if I try to print the Objs by Dumper function, my script crashes. No where I am making any direct call / or use of any of Net::SSH2 internal variables so as to corrupt it. Q. Why is Obj2/Obj3 corrupting? I am unable to understand if corruption is because of changes made in “a”, or some other issue.

  • Comment on call to Net::SSH2 functions crashes caller object

Replies are listed 'Best First'.
Re: call to Net::SSH2 functions crashes caller object
by salva (Canon) on Sep 20, 2013 at 09:32 UTC
    If you get a SEGV error that means that there is some bug on Net::SSH2, or in some other XS module or in perl.

    Try to write a minimal test script that still triggers the error and then send it as a bug report to Net::SSH2 maintainer. He is quite fast fixing bugs on Net::SSH2!

    Otherwise, as it seems you are running your script in some kind of Unix/Linux system, you may like to switch to Net::OpenSSH.

      Hi, Thanks for reply, my 1st question, is there really a need to make that lexical variable %Event (defined in Net::SSH2.pm global ?

        Trying to fix something you don't understand by randomly changing related things is not going to be very productive.

        Try to find where and why the variable gets corrupted and then write a proper patch, or just do as I toll you on my previous post and send a bug report to the author.

        Anyway, I find hard to imagine a bug causing a SEGV error that could be fixed by just changing a package variable from lexical to global.

        Is your program using threads?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1054979]
Approved by salva
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 06:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found