From tech@gznianguan.com  Thu Jun 12 19:17:38 2014
Received: (at 302) by bugs.x2go.org; 12 Jun 2014 17:17:40 +0000
X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on
	ymir.das-netzwerkteam.de
X-Spam-Level: 
X-Spam-Status: No, score=0.0 required=5.0 tests=BAYES_20,URIBL_BLOCKED
	autolearn=ham version=3.3.2
X-Greylist: delayed 1155 seconds by postgrey-1.34 at ymir.das-netzwertkeam.de; Thu, 12 Jun 2014 19:17:37 CEST
Received: from mailstore06.sysedata.no (b.mail.tornado.no [195.159.29.130])
	by ymir.das-netzwerkteam.de (Postfix) with ESMTPS id EC5F25DB11
	for <302@bugs.x2go.org>; Thu, 12 Jun 2014 19:17:37 +0200 (CEST)
Received: from [119.129.50.253] (helo=[10.0.0.9])
	by mailstore06.sysedata.no with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16)
	(Exim 4.71)
	(envelope-from <tech@gznianguan.com>)
	id 1Wv8KS-0007ur-Rd
	for 302@bugs.x2go.org; Thu, 12 Jun 2014 18:58:21 +0200
Message-ID: <5399DC29.5000408@gznianguan.com>
Date: Thu, 12 Jun 2014 18:58:17 +0200
From: GZNG Tech <tech@gznianguan.com>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0
MIME-Version: 1.0
To: 302@bugs.x2go.org
Subject: Possible solution to #302 ?
Content-Type: multipart/mixed;
 boundary="------------060906020402010001030108"

This is a multi-part message in MIME format.
--------------060906020402010001030108
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Control: tag -1 patch

Hi,

Putting the attached file into your "extensions/post-suspend.d" folder
on the server...
may solve #302 issues...

On Ubuntu that would be an executable named "302_bug-fix" with the
following full path:
/usr/lib/x2go/extensions/post-suspend.d/302_bug-fix
(On other distros just simply locate your "post-suspend.d" folder and
put it there..
PS! Make sure its executable! (chmod 755).

Its may be good to clean up all old sessions before you
start a brand new and fresh session to test this fix.

We were able to reliably reproduce fault/non-fault conditions and after
applying this fix, we're seeing 100% success rates for session resume
with pyhoca....


The source of the issue seem to be that the NX port reappears on the
server within seconds of session suspension.... in  a state where where
its not ready for new incoming connections... (quite frankly it should
not be available at all at this time, should it?)  So either we're
reconnecting to this "faulty" port
or the fresh port fails to open due to the "old port" being in the
way....  Either way NX fails to resume...

The problem seem to be consistently occurring when rapidly
suspending/resuming with pyhoca and if x2goclient experience
abnormal/unclean disconnects that results in suspension.

Either way this little perl script will clean up those stray NX ports by
simply locating and connecting to them... (after which, the port closes,
as it should.)

If this indeed solves the #302 bug, this should probably move from
extensions to being part of the suspend scripts....  but for cleanliness
and admin-friendliness its easier to test it as an extension....

Let us know if this works for you!

(btw: If the supportive subs are adopted to Utils.pm  the script it self
would become quite compact)



Best Regards,
-GZNGET











--------------060906020402010001030108
Content-Type: text/plain; charset=UTF-8;
 name="302_bug-fix"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment;
 filename="302_bug-fix"

#!/usr/bin/perl 
# We'd love to run this with  -T  but Utils.pm and probably DB.pm too need some work before that can happen..
########################################################################################################################
#
# Copyright (C) 2013-2014 X2Go Project - http://wiki.x2go.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Copyright (C) 2013-2014 Guangzhou Nianguan Electronics Technology Co.Ltd. <opensource@gznianguan.com>
# 
#########################################################################################################################
#
# Hopefully this solves the issues pretaining to the #302 session resume bug.
# If so, we dedicate this bugfix to the Logitech D250 Keyboard who's servicelife came to an abrupt end due to this bug.
#
#########################################################################################################################
use strict;
use Sys::Hostname;
use IO::Socket;
use X2Go::Utils qw( sanitizer );
use X2Go::Server::DB qw(db_listsessions);

my $doRetryCnt = 5;
my $X2GoSID = requireX2GOSID_argvenvdie();


my @X2GoSesINFO = getSessionInfo($X2GoSID);
if (@X2GoSesINFO[1] eq $X2GoSID) {
  while ($doRetryCnt > 0) {
    $doRetryCnt--;
    if (itsUP($X2GoSesINFO[8])) {
      exit;
    } 
    sleep 1;
  }
} 


########################################################################################################
# Only SUBS past this point....: (Any Utils.pm candidates?)

sub requireX2GOSID_argvenvdie { # Return X2Go SID from either ARGV[0] or ENV.... terminates on failure.
				# If this ends up moving to Utils.pm, renaming may be a good idea.
  if (sanitizer("x2gosid",@ARGV[0])) {
    return sanitizer("x2gosid",@ARGV[0]);
  } elsif (sanitizer("x2gosid",$ENV{'X2GO_SESSION'})) {
    return sanitizer("x2gosid",$ENV{'X2GO_SESSION'});
  } else { 
    die "No X2Go Session ID in ARGV or ENV!\n"; 
  }
}

sub itsUP { # Simple sub for checking if the port is up on localhost...
	    # If this ends up moving to Utils.pm, renaming may be a good idea.
  my $portNum = sanitizer("num",$_[0]) or die "Port number not a number '$_[0]'?";
  my $socket = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => $portNum, Proto    => 'tcp');
  if (defined $socket) { 
    print $socket "\n";
    $socket->close; 
    return 1;
  } else {
    return 0;
  }
}


sub getSessionInfo { # Simple sub to get X2GoSessionINFO from the database (kind of like greping it out of "x2golistsessions" without cranking another perl script)...
   	             # If this ends up moving to Utils.pm, renaming may be a good idea.
  my $theX2GoSID = $_[0];
  foreach  my $sessionLine (db_listsessions(hostname)) {
    if ($sessionLine =~ /$theX2GoSID/) {
      return split(/\|/,$sessionLine);
    }
  }
  return 0;
}




--------------060906020402010001030108--

