xebialabs / overthere

Runs something "Over there"

Home Page:http://www.xebialabs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

com.hierynomus.smbj.Session.connectShare() can return a closed Share

zerikv opened this issue · comments

When the PacketReader catches an unexpected error (like a Connection reset by peer) the com.hierynomus.smbj.connection.Connection is closed:

14:59:43.964 INFO  com.hierynomus.smbj.transport.PacketReader#run - PacketReader error.
14:59:43.964 INFO  com.hierynomus.smbj.session.Session#logoff - Logging off session 39646104911945 from host WIN10
14:59:43.964 DEBUG com.hierynomus.smbj.connection.Connection#send - Granted 1 (out of 2048) credits to SMB2_TREE_DISCONNECT with message id << 26322 >>
14:59:43.964 DEBUG com.hierynomus.smbj.transport.tcp.direct.DirectTcpTransport#write - Writing packet SMB2_TREE_DISCONNECT with message id << 26322 >>
14:59:43.964 ERROR com.hierynomus.smbj.session.Session#logoff - Caught exception while closing TreeConnect with id: 1
com.hierynomus.protocol.transport.TransportException: java.net.SocketException: Connection reset by peer: socket write error
	at com.hierynomus.smbj.transport.tcp.direct.DirectTcpTransport.write(DirectTcpTransport.java:77)
	at com.hierynomus.smbj.connection.Connection.send(Connection.java:250)
	at com.hierynomus.smbj.session.Session.send(Session.java:211)
	at com.hierynomus.smbj.share.TreeConnect.close(TreeConnect.java:61)
	at com.hierynomus.smbj.share.Share.close(Share.java:90)
	at com.hierynomus.smbj.session.Session.logoff(Session.java:160)
	at com.hierynomus.smbj.session.Session.close(Session.java:193)
	at com.hierynomus.smbj.connection.Connection.close(Connection.java:135)
	at com.hierynomus.smbj.connection.Connection.close(Connection.java:120)
	at com.hierynomus.smbj.connection.Connection.handleError(Connection.java:414)
	at com.hierynomus.smbj.transport.PacketReader.run(PacketReader.java:53)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketException: Connection reset by peer: socket write error
	at java.net.SocketOutputStream.socketWrite0(Native Method)
	at java.net.SocketOutputStream.socketWrite(Unknown Source)
	at java.net.SocketOutputStream.write(Unknown Source)
	at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
	at java.io.BufferedOutputStream.flush(Unknown Source)
	at com.hierynomus.smbj.transport.tcp.direct.DirectTcpTransport.write(DirectTcpTransport.java:74)
	... 11 common frames omitted

In cascade the Connection closes all SMB Sessions, and each session closes all shares.

After the method Session.connectShare() returns a closed Share because com.xebialabs.overthere.smb.SmbConnection uses the session without tests before if the connection has been closed:

com.hierynomus.smbj.common.SMBRuntimeException: DiskShare has already been closed
	at com.hierynomus.smbj.share.Share.send(Share.java:336)
	at com.hierynomus.smbj.share.Share.sendReceive(Share.java:330)
	at com.hierynomus.smbj.share.Share.doCreateFile(Share.java:146)
	at com.hierynomus.smbj.share.Share.createFile(Share.java:124)
	at com.hierynomus.smbj.share.DiskShare.open(DiskShare.java:55)
	at com.hierynomus.smbj.share.DiskShare.openDirectory(DiskShare.java:78)
	at com.hierynomus.smbj.share.DiskShare.list(DiskShare.java:165)
	at com.hierynomus.smbj.share.DiskShare.list(DiskShare.java:138)
	at com.xebialabs.overthere.smb.SmbFile.listFiles(SmbFile.java:275)
	at com.xebialabs.overthere.OverthereFile$listFiles.call(Unknown Source)

@hierynomus : maybe the SMB Session should clean (in the method logoff) the cache of Shares (TreeConnectTable.closed is never called),
and it would be perhaps more clear if the public methods in the SMB Connection which use Transport throw an explicit Exception (like SMBConnectionClosedException) when the transport has been disconnected (and the same for SMB Session for public methods which need the connection)

Anyway it seems that the com.xebialabs.overthere.smb.SmbConnection has to test if the com.hierynomus.smbj.connection.Connection has been closed before get the share, or I'm on a wrong way?

@zerikv I think I have fixed this in SMBJ now.