Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Background proc requires ENTER keystroke on Windows 2012

by stevieb (Canon)
on Aug 01, 2016 at 16:42 UTC ( [id://1168938]=perlquestion: print w/replies, xml ) Need Help??

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

Disclaimer: cross-posted at StackOverflow.

This is a much simpler (pure Perl) version of Debug background Perl procs started with compiled C# code (breaks only on AWS), as its far less reading, and much simpler to test/reproduce.

The issue is that on my Amazon Web Services (AWS) Windows 2012 systems, a Perl script that runs in the background listening for network requests requires an ENTER keystroke in the server script (background proc) cmd window before it processes. This does not happen on my Windows 2008 R2 systems (the proc runs properly with no interaction).

I am not certain if this is an AWS thing, or a win2k12 thing, as I don't have any 2k12 servers to test on that aren't in AWS. I'd appreciate if anyone with 2k12 systems could test this to see if it works properly.

This test does not require any non-core modules to be installed, and I've removed the requirement for Proc::Background as well.

To repro:

- copy below code to client.pl and server.pl respectively - start the server in the background: -- perl server.pl bg - run the client (different cmd window): -- perl client.pl

On my local servers, the server returns to the client properly without any interaction. On my AWS systems, it hangs. Hitting ENTER in the server cmd window causes things to go through. Running the server script in foreground mode (perl server.pl run), the whole shebang works properly on 2012 with no interaction.

NOTE: there's no method in this test code to stop the server.pl background proc. It has to be killed via Task Manager.

client.pl

use warnings; use strict; use IO::Socket::INET; use Storable; my $mod = 'IO::Socket::INET'; my $sock = new IO::Socket::INET ( PeerHost => 'localhost', PeerPort => 7800, Proto => 'tcp', ); die "can't create socket\n" unless $sock; $sock->send("cpanm $mod"); my $recv = Storable::fd_retrieve($sock); print $$recv;

server.pl

use warnings; use strict; use IO::Socket::INET; use Storable; if (@ARGV && $ARGV[0] eq 'bg'){ system 1, 'perl', $0, 'run'; } if (@ARGV && $ARGV[0] eq 'run') { my $sock = new IO::Socket::INET ( LocalHost => '0.0.0.0', LocalPort => 7800, Proto => 'tcp', Listen => 5, Reuse => 1, ); die "cannot create socket $!\n" unless $sock; while (1){ my $conn = $sock->accept; my $cmd; $conn->recv($cmd, 1024); print "executing: $cmd\n"; my $ret = `$cmd`; print "return: $ret\n"; Storable::nstore_fd(\$ret, $conn); shutdown($conn, 1); } $sock->close; }

Replies are listed 'Best First'.
Re: Background proc requires ENTER keystroke on Windows 2012 (details)
by tye (Sage) on Aug 01, 2016 at 17:51 UTC

    Since I can't easily reproduce the problem behavior, could you at least note what the bg server produces before you press Enter? If nothing (that is, if the "executing:" line is not printed until after you press Enter), then you should add more output so that you can narrow down where the hang is happening. If "executing:" is displayed before you hit Enter, do you have to hit Enter again in order to get a second request to finish?

    - tye        

      The latter, tye.

      "executing: ..." is displayed before I hit ENTER. This is after I see in Task Manager a new perl.exe process start up. Once it does, the "executing..." line is displayed, then I must hit enter for the entire process to proceed.

        You can probably prevent this problem by "backgrounding" your daemon the proper way, by daemonizing it. No, Proc::Background does not do that either.

        If you at least re-open STDIN, STDOUT, and STDERR to/from File::Spec->devnull() (or even have the last two append to some log file), then I will be a bit surprised if the daemon has the ability to ask for input from the calling context.

        Some of the daemonize modules might even know the right trick for completely disassociating the daemon from the parent's "console" (similar to what motivates the setsid() part of daemonizing on Unix). If not, you could try calling Free() in Win32::Console, though I don't recall having tried that and I have not swapped back in most of what little I used to know about Win32 "consoles".

        If I wanted to try and debug what is happening, then I'd probably next look for a compatible version of "process monitor" such as one from the former SysInternals.

        - tye        

Re: Background proc requires ENTER keystroke on Windows 2012
by BrowserUk (Patriarch) on Aug 01, 2016 at 19:11 UTC

    Sorry, I got stuck trying to get cpanm to run in my system; without success. (What a steaming pile!)

    Have you tried substituting some other command for cpanm? If the substitute doesn't hang it would indicate that it is cpanm; probably a difference in configuration between the systems.

    Guesswork here, but from my previous experiences with cpan & cpanp; they tend to make extensive use of Term::* for keyboard handling; providing (crap) history and supposedly "pretty" coloring that makes the output look like a '90s leet hAxoR website -- ie. totally unreadable... {rant grossly abbreviated}

    My point is that those modules only seem to "work" on windows if the cmd window sessions are configured with the default settings -- eg. 80x25 screen white on black; no quickedit or tab completion etc. etc. Settings that no one who actually use windows would ever stick with.

    My best guess from reading the symptoms is that AWS cmd sessions are not configured with those useless defaults and so Term::* et.al. conflict.

    The easy way to test is to replace cpanm with a simple perl script by the same name that just reads the arguments and echos them to both stdout and stderr.

    If that works without hanging, you know where to look and what to look for.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      If hanging is a problem, a set PERL_MM_USE_DEFAULT=1 might help, as well as adding --notest

      It isn't just cpanm that hangs. On the server cmd window, I get the executing: ... output, but then to get the output from the backticks command, I have to hit ENTER.

      I've been in and out this aft, but when I fully return, I'll test whether the code in the backticks runs and just doesn't return, or ENTER needs to be pressed before the backticks code executes. I'm assuming the former (code runs, but doesn't return without ENTER).

      I've tried a few things... including running a CLI instance of perl to output a simple string of text... same result; have to hit ENTER.

        but then to get the output from the backticks command, I have to hit ENTER.

        What happens if you add a \n in the backticks?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Background proc requires ENTER keystroke on Windows 2012
by SimonPratt (Friar) on Aug 02, 2016 at 14:18 UTC

    Switching out "cpanm $mod" with "dir" allows your code to work perfectly on an AWS Windows 2012 server I have access to here. As we don't have cpanm, I can't test it with that specific command.

    I expect that either cpanm is requiring the additional keypress, or you are running afoul of one or more Windows security settings (are you running in an Administrator command prompt?)

      Thanks for testing.

      On both my local 2k8r2 boxes, and AWS 2012r2, I'm running as an admin.

      I have the exact same setup both locally and on AWS. I'm in the process of installing a 2012 VM and will test there, and if it works, will upgrade it to R2 and test again. If it works on 2012R2 locally, I know the problem is with Amazon.

      I don't think it's cpanm... running the script in the foreground allows it to go through without any interaction. I'll test with the dir command in my setup and see what happens.

      update: I confirm that sending in the "dir" command works as I would expect on my AWS instances. Weird.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-19 11:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found