Issue #16413 has been reported by koshigoe (Masataka SUZUKI).

----------------------------------------
Bug #16413: Net::FTP#put で書き込みエラーが発生した時の CLOSE_WAIT
https://bugs.ruby-lang.org/issues/16413

* Author: koshigoe (Masataka SUZUKI)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.4p104 (2019-08-28 revision 67798) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
### 疑問

`Net::FTP#storbinary` などをみると、 `conn.write` で例外が発生した際に `conn.close` を呼ばない実装だと思います。
https://github.com/ruby/ruby/blob/7be550d046c726c2a3aa625ceb260d9b2268fb5a/lib/net/ftp.rb#L683-L707

一方で `Net::FTP#retrbinary` では `conn.shutdown` や `conn.close` (`ensure`) をしています。
https://github.com/ruby/ruby/blob/7be550d046c726c2a3aa625ceb260d9b2268fb5a/lib/net/ftp.rb#L629-L648

素人的な質問で恐縮ですが、これは意図的な実装でしょうか?

### 確認内容

適当な FTP サーバを起動。

```
$ docker run --rm \
  -p 20-21:20-21 \
  -p 21100-21110:21100-21110 \
  -e FTP_USER=user \
  -e FTP_PASS=pass \
  -e PASV_ADDRESS=localhost \
  -e PASV_MIN_PORT=21100 \
  -e PASV_MAX_PORT=21110 \
  fauria/vsftpd
```

irb で `Net::FTP#put` のエラーを繰り返し発生させる。

```
$ ruby -v
ruby 2.6.4p104 (2019-08-28 revision 67798) [x86_64-darwin18]
$ irb -rnet/ftp
> ftp = Net::FTP.new
> ftp.passive = true
> ftp.connect('localhost')
> ftp.login('user', 'pass')
> ftp.put('/dev/null', '/null')     # 正常
> ftp.put('/dev/null', '/nil/null') # エラー
... エラーを繰り返してみる ...
```

エラー時は `CLOSE_WAIT`, `FIN_WAIT2` が増えていく。

```
$ lsof -i:21100-21110
COMMAND     PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
com.docke  1354 koshigoe   17u  IPv6 0x370c967241f8be15      0t0  TCP *:21110 (LISTEN)
com.docke  1354 koshigoe   19u  IPv6 0x370c967241f8dad5      0t0  TCP *:21109 (LISTEN)
com.docke  1354 koshigoe   21u  IPv6 0x370c967241f8f1d5      0t0  TCP *:21108 (LISTEN)
com.docke  1354 koshigoe   23u  IPv6 0x370c967241f8cf55      0t0  TCP *:21107 (LISTEN)
com.docke  1354 koshigoe   25u  IPv6 0x370c967241f8e095      0t0  TCP *:21106 (LISTEN)
com.docke  1354 koshigoe   29u  IPv6 0x370c967241f8c3d5      0t0  TCP *:21105 (LISTEN)
com.docke  1354 koshigoe   31u  IPv6 0x370c967241f8c995      0t0  TCP *:21104 (LISTEN)
com.docke  1354 koshigoe   33u  IPv6 0x370c967241f8d515      0t0  TCP *:21103 (LISTEN)
com.docke  1354 koshigoe   35u  IPv6 0x370c967241a28e15      0t0  TCP *:21102 (LISTEN)
com.docke  1354 koshigoe   37u  IPv6 0x370c967241a28855      0t0  TCP *:21101 (LISTEN)
com.docke  1354 koshigoe   39u  IPv6 0x370c967241a2a515      0t0  TCP *:21100 (LISTEN)
com.docke  1354 koshigoe   44u  IPv6 0x370c9672385ae1d5      0t0  TCP localhost:21110->localhost:53362 (FIN_WAIT_2)
com.docke  1354 koshigoe   45u  IPv6 0x370c9672385ad655      0t0  TCP localhost:21107->localhost:53363 (FIN_WAIT_2)
com.docke  1354 koshigoe   46u  IPv6 0x370c9672434e7855      0t0  TCP localhost:21102->localhost:53378 (FIN_WAIT_2)
com.docke  1354 koshigoe   48u  IPv6 0x370c9672434e83d5      0t0  TCP localhost:21100->localhost:53379 (FIN_WAIT_2)
com.docke  1354 koshigoe   49u  IPv6 0x370c9672434eac15      0t0  TCP localhost:21105->localhost:53380 (FIN_WAIT_2)
com.docke  1354 koshigoe   50u  IPv6 0x370c967241f8ec15      0t0  TCP localhost:21108->localhost:53381 (FIN_WAIT_2)
com.docke  1354 koshigoe   51u  IPv6 0x370c967241a29f55      0t0  TCP localhost:21107->localhost:53382 (FIN_WAIT_2)
com.docke  1354 koshigoe   52u  IPv6 0x370c9672385aa855      0t0  TCP localhost:21109->localhost:53383 (FIN_WAIT_2)
com.docke  1354 koshigoe   53u  IPv6 0x370c967255892f55      0t0  TCP localhost:21109->localhost:53384 (FIN_WAIT_2)
ruby      66185 koshigoe   15u  IPv6 0x370c9672385adc15      0t0  TCP localhost:53362->localhost:21110 (CLOSE_WAIT)
ruby      66185 koshigoe   16u  IPv6 0x370c9672385ab995      0t0  TCP localhost:53363->localhost:21107 (CLOSE_WAIT)
ruby      66185 koshigoe   17u  IPv6 0x370c9672434eb1d5      0t0  TCP localhost:53378->localhost:21102 (CLOSE_WAIT)
ruby      66185 koshigoe   18u  IPv6 0x370c9672434ea095      0t0  TCP localhost:53379->localhost:21100 (CLOSE_WAIT)
ruby      66185 koshigoe   19u  IPv6 0x370c9672434e9515      0t0  TCP localhost:53380->localhost:21105 (CLOSE_WAIT)
ruby      66185 koshigoe   20u  IPv6 0x370c967241f8e655      0t0  TCP localhost:53381->localhost:21108 (CLOSE_WAIT)
ruby      66185 koshigoe   21u  IPv6 0x370c967241a2b655      0t0  TCP localhost:53382->localhost:21107 (CLOSE_WAIT)
ruby      66185 koshigoe   22u  IPv6 0x370c967241a2c1d5      0t0  TCP localhost:53383->localhost:21109 (CLOSE_WAIT)
ruby      66185 koshigoe   23u  IPv6 0x370c9672385abf55      0t0  TCP localhost:53384->localhost:21109 (CLOSE_WAIT)
```




-- 
https://bugs.ruby-lang.org/