You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Kamil (JIRA)" <ji...@apache.org> on 2007/08/22 22:08:30 UTC

[jira] Created: (NET-168) NullPointerException in TelnetClient.disconnect() while being connected

NullPointerException in TelnetClient.disconnect() while being connected
-----------------------------------------------------------------------

                 Key: NET-168
                 URL: https://issues.apache.org/jira/browse/NET-168
             Project: Commons Net
          Issue Type: Bug
    Affects Versions: 1.4
         Environment: Debian GNU/Linux, FedoraCore, RedHat
            Reporter: Kamil


java.lang.NullPointerException
org.apache.commons.net.telnet.TelnetClient.disconnect(TelnetClient.java:125)
org.apache.commons.net.ftp.FTP.disconnect(FTP.java:397)
org.apache.commons.net.ftp.FTPClient.disconnect(FTPClient.java:590)

This exception occurs when trying to invoke myFTPClient.disconnect() method, when ftpclient hangs for a long time and dont want to connect...

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (NET-168) NullPointerException in TelnetClient.disconnect() while being connected

Posted by "Nathan Beyer (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/NET-168?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12523150 ] 

Nathan Beyer commented on NET-168:
----------------------------------

Based on that code snippet, I would say that you have a concurrency issue. It's even presenting as a concurrency issue - it happens rarely and sporadically.

FTPClient is not thread safe, you can't invoke methods on it concurrently, which your code appears to be doing. The 'testHandler' and 'interrupt' method are being invoked on two different threads, correct? If you want to maintain the FTPClient as a instance variable, you'll have to synchronize access to it; only one thread can be interacting with it at a time. Note, access to the field itself must be synchronized as well, but you can generally do this with the same lock. Are you reusing instances of FTPClient amongst different threads?

To get a better understanding of what's happening, I would suggest putting some tracing in this class to see when, how many times and on what thread the methods are getting invoked. I think you'll be surprised as to what you see.

> NullPointerException in TelnetClient.disconnect() while being connected
> -----------------------------------------------------------------------
>
>                 Key: NET-168
>                 URL: https://issues.apache.org/jira/browse/NET-168
>             Project: Commons Net
>          Issue Type: Bug
>    Affects Versions: 1.4
>         Environment: Debian GNU/Linux, FedoraCore, RedHat
>            Reporter: Kamil
>
> java.lang.NullPointerException
> org.apache.commons.net.telnet.TelnetClient.disconnect(TelnetClient.java:125)
> org.apache.commons.net.ftp.FTP.disconnect(FTP.java:397)
> org.apache.commons.net.ftp.FTPClient.disconnect(FTPClient.java:590)
> This exception occurs when trying to invoke myFTPClient.disconnect() method, when ftpclient hangs for a long time and dont want to connect...

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (NET-168) NullPointerException in TelnetClient.disconnect() while being connected

Posted by "Kamil (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/NET-168?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12522951 ] 

Kamil commented on NET-168:
---------------------------

yes, i do.
Here is class which is using FTPClient:
public class FtpTester{
	private FTPClient ftpCon=null;
	public void testHandler(String host,long timeout,Authentication auth) {
		long startTime = 0;
		long totalTime = 0;
		long startDownloadTime = 0;
		long totalDownloadTime = 0;
		String res = "";
		int resp=0;

		String exception=null;

		try {
			ftpCon=new FTPClient();
			startTime = System.currentTimeMillis();
			ftpCon.setDefaultTimeout((int) timeout);
			ftpCon.connect(host,21);

			String user=null;
			String pass=null;
			if(auth!=null){
				user=auth.getUsername();
				pass=auth.getPassword();
			}
			if(user!=null && pass!=null){
				ftpCon.user(user);
				ftpCon.pass(pass);
			}
			else{
				ftpCon.user("anonymous");
				ftpCon.pass("");
			}
			startDownloadTime = System.currentTimeMillis();
			res=ftpCon.getReplyString();
			resp= ftpCon.getReplyCode();
			totalDownloadTime = System.currentTimeMillis()-startDownloadTime;
			totalTime = System.currentTimeMillis()-startTime;

		}catch (Exception e) {
			e.printStackTrace();
                        exception=e.getMessage();
		}finally{
			try {
				if(ftpCon!=null && ftpCon.isConnected()){
					//ftpCon.logout();
					ftpCon.disconnect(); //HERE occured an Exception
				}
			} catch (IOException e) {
			}
		}
	}
	public void interrupt() {//used in different thread if connection hangs for a long time
		try {
			if(ftpCon!=null&& ftpCon.isConnected()){
				ftpCon.disconnect();
			}
		} catch (IOException e) {
		}
	}
}

It's not so common - it happens once a week maybe, but it's strange.
While this exception happens - as you see - i'm disconnecting FTPClient.
It's a server, so in other threads i have Http connections running, but it shouldn't matter. Server connects to ftp do some connection tests. It's running all the time.
I Hope that it would help. 
Regards

---- Wiadomość Oryginalna ----
Od: "Nathan Beyer (JIRA)" <ji...@apache.org>
Do: eximius@o2.pl
Data: 27 sierpnia 2007 3:21
Temat: [jira] Commented: (NET-168) NullPointerException in  TelnetClient.disconnect() while being connected




> NullPointerException in TelnetClient.disconnect() while being connected
> -----------------------------------------------------------------------
>
>                 Key: NET-168
>                 URL: https://issues.apache.org/jira/browse/NET-168
>             Project: Commons Net
>          Issue Type: Bug
>    Affects Versions: 1.4
>         Environment: Debian GNU/Linux, FedoraCore, RedHat
>            Reporter: Kamil
>
> java.lang.NullPointerException
> org.apache.commons.net.telnet.TelnetClient.disconnect(TelnetClient.java:125)
> org.apache.commons.net.ftp.FTP.disconnect(FTP.java:397)
> org.apache.commons.net.ftp.FTPClient.disconnect(FTPClient.java:590)
> This exception occurs when trying to invoke myFTPClient.disconnect() method, when ftpclient hangs for a long time and dont want to connect...

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (NET-168) NullPointerException in TelnetClient.disconnect() while being connected

Posted by "Nathan Beyer (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/NET-168?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12522917 ] 

Nathan Beyer commented on NET-168:
----------------------------------

Do you have a code example or test case that can recreate this behavior? Can you provide any more context to what you're doing when this happens?

Are you accessing an instance of TelnetClient, FTP or FTPClient from multiple threads? If so, are you protecting it appropriately?

> NullPointerException in TelnetClient.disconnect() while being connected
> -----------------------------------------------------------------------
>
>                 Key: NET-168
>                 URL: https://issues.apache.org/jira/browse/NET-168
>             Project: Commons Net
>          Issue Type: Bug
>    Affects Versions: 1.4
>         Environment: Debian GNU/Linux, FedoraCore, RedHat
>            Reporter: Kamil
>
> java.lang.NullPointerException
> org.apache.commons.net.telnet.TelnetClient.disconnect(TelnetClient.java:125)
> org.apache.commons.net.ftp.FTP.disconnect(FTP.java:397)
> org.apache.commons.net.ftp.FTPClient.disconnect(FTPClient.java:590)
> This exception occurs when trying to invoke myFTPClient.disconnect() method, when ftpclient hangs for a long time and dont want to connect...

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Closed: (NET-168) NullPointerException in TelnetClient.disconnect() while being connected

Posted by "Rory Winston (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/NET-168?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Rory Winston closed NET-168.
----------------------------

    Resolution: Invalid

Looks like a concurrency issue, unless shown to be otherwise.

> NullPointerException in TelnetClient.disconnect() while being connected
> -----------------------------------------------------------------------
>
>                 Key: NET-168
>                 URL: https://issues.apache.org/jira/browse/NET-168
>             Project: Commons Net
>          Issue Type: Bug
>    Affects Versions: 1.4
>         Environment: Debian GNU/Linux, FedoraCore, RedHat
>            Reporter: Kamil
>
> java.lang.NullPointerException
> org.apache.commons.net.telnet.TelnetClient.disconnect(TelnetClient.java:125)
> org.apache.commons.net.ftp.FTP.disconnect(FTP.java:397)
> org.apache.commons.net.ftp.FTPClient.disconnect(FTPClient.java:590)
> This exception occurs when trying to invoke myFTPClient.disconnect() method, when ftpclient hangs for a long time and dont want to connect...

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (NET-168) NullPointerException in TelnetClient.disconnect() while being connected

Posted by "Kamil (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/NET-168?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12539808 ] 

Kamil commented on NET-168:
---------------------------

You mean, that i connot invoke disconnect() method from another thread?? It's a little bit strange since it is no problem for java Socket to invoke close() method in that way...

Regards

---- Wiadomość Oryginalna ----
Od: "Nathan Beyer (JIRA)" <ji...@apache.org>
Do: eximius@o2.pl
Data: 28 sierpnia 2007 5:55
Temat: [jira] Commented: (NET-168) NullPointerException in  TelnetClient.disconnect() while being connected




> NullPointerException in TelnetClient.disconnect() while being connected
> -----------------------------------------------------------------------
>
>                 Key: NET-168
>                 URL: https://issues.apache.org/jira/browse/NET-168
>             Project: Commons Net
>          Issue Type: Bug
>    Affects Versions: 1.4
>         Environment: Debian GNU/Linux, FedoraCore, RedHat
>            Reporter: Kamil
>
> java.lang.NullPointerException
> org.apache.commons.net.telnet.TelnetClient.disconnect(TelnetClient.java:125)
> org.apache.commons.net.ftp.FTP.disconnect(FTP.java:397)
> org.apache.commons.net.ftp.FTPClient.disconnect(FTPClient.java:590)
> This exception occurs when trying to invoke myFTPClient.disconnect() method, when ftpclient hangs for a long time and dont want to connect...

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (NET-168) NullPointerException in TelnetClient.disconnect() while being connected

Posted by "Nathan Beyer (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/NET-168?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12539981 ] 

Nathan Beyer commented on NET-168:
----------------------------------

You can call disconnect from another thread, but you must do so exclusive of any other interaction on the FTPClient instance. You must synchronize access. You may be able to do this sort of thing with java.net.Socket, but it likely has some form of internal synchronization to protect things; however, there's no mention of this in the javadoc, so I wouldn't make any assumptions about that.

In any case, what you'll want to do is something like this.

// create a final lock object to synchronize access to the 'ftpCon' field
private FTPClient ftpCon = null;
private final Object ftpConLock = new Object();

public void testHandler() {
    ...
    // synchronize every access to the field
    synchronized(ftpConLock) {
        ftpCon = new FTPClient();
        ...
    }
   ...
}

public void interrupt() {
   synchronized(ftpConLock) {
      if (ftpCon != null && ftpCon.isConnected()) {
         ftpCon.disconnect();
      }
   }
}

> NullPointerException in TelnetClient.disconnect() while being connected
> -----------------------------------------------------------------------
>
>                 Key: NET-168
>                 URL: https://issues.apache.org/jira/browse/NET-168
>             Project: Commons Net
>          Issue Type: Bug
>    Affects Versions: 1.4
>         Environment: Debian GNU/Linux, FedoraCore, RedHat
>            Reporter: Kamil
>
> java.lang.NullPointerException
> org.apache.commons.net.telnet.TelnetClient.disconnect(TelnetClient.java:125)
> org.apache.commons.net.ftp.FTP.disconnect(FTP.java:397)
> org.apache.commons.net.ftp.FTPClient.disconnect(FTPClient.java:590)
> This exception occurs when trying to invoke myFTPClient.disconnect() method, when ftpclient hangs for a long time and dont want to connect...

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.