Issue #17933 has been updated by miguelfteixeira (Miguel Teixeira).


I've created a pull request in the [Ruby Github project](https://github.com/ruby/ruby) with the proposed solution: https://github.com/ruby/ruby/pull/4564


----------------------------------------
Bug #17933: `Net::HTTP#write_timeout` doesn't work with `body_stream`
https://bugs.ruby-lang.org/issues/17933#change-92419

* Author: miguelfteixeira (Miguel Teixeira)
* Status: Open
* Priority: Normal
* ruby -v: ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]
* Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
While testing `Net::HTTP#write_timeout` using the server example from the [Feature Issue](https://bugs.ruby-lang.org/issues/13396), I've noticed that [Faraday](https://github.com/lostisland/faraday) multipart requests (with a file parameter) didn't trigger `WriteTimeout`. The root cause is that [Net::HTTPGenericRequest#send_request_with_body_stream](https://github.com/ruby/ruby/blob/v2_6_7/lib/net/http/generic_request.rb#L205-L207) is not using `Net::BufferedIO` (the class that implements the write timeout).

The following patch fixes the issue, but looking at the comments, it's unclear if it will break some existing functionality. Albeit all the tests pass.

```diff
--- a/lib/net/http/generic_request.rb
+++ b/lib/net/http/generic_request.rb
@@ -204,7 +204,7 @@ def send_request_with_body_stream(sock, ver, path, f)
     else
       # copy_stream can sendfile() to sock.io unless we use SSL.
       # If sock.io is an SSLSocket, copy_stream will hit SSL_write()
-      IO.copy_stream(f, sock.io)
+      IO.copy_stream(f, sock)
     end
   end
```

```bash
  ruby git:(fix-net-http-write-timeout-body-stream)  ../mspec/bin/mspec run library/net/
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]
[- | ==================100%================== | 00:00:00]      0F      0E

Finished in 2.029623 seconds

187 files, 876 examples, 1214 expectations, 0 failures, 0 errors, 0 tagged
```

Any thoughts on this? It looks like `Net::HTTP#write_timeout` is partially implemented, and we should ensure that it either works on all use cases or an error is raised when it's used in a non-supported use case.

I'm more than happy to help with the implementation. If this patch is the correct approach, I can also create new unit tests to assert the proper behaviour.



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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>