From c195322ff56dfb5e0a303640dab0e9baa8cdea30 Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Thu, 27 Aug 2026 23:54:57 +0300 Subject: [PATCH 1/2] Reject nested SMTP starts before entering session cleanup --- lib/net/smtp.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb index cca06e6..cd8af89 100644 --- a/lib/net/smtp.rb +++ b/lib/net/smtp.rb @@ -621,6 +621,7 @@ def started? # def start(*args, helo: nil, user: nil, secret: nil, password: nil, authtype: nil) raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0..4)" if args.size > 4 + raise IOError, 'SMTP session already started' if @started helo ||= args[0] || 'localhost' user ||= args[1] secret ||= password || args[2] @@ -664,7 +665,6 @@ def tcp_socket(address, port) end def do_start(helo_domain, user, secret, authtype) - raise IOError, 'SMTP session already started' if @started if user || secret || authtype check_auth_args authtype, user, secret end From 65d128373d42ae4e62714ec8accdd43afcf7c1ea Mon Sep 17 00:00:00 2001 From: Oskar Eichler <62393985+OskarEichler@users.noreply.github.com> Date: Fri, 28 Aug 2026 15:24:17 +0300 Subject: [PATCH 2/2] Add regression coverage for reject nested smtp starts before entering session cleanup --- test/net/smtp/test_smtp.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/net/smtp/test_smtp.rb b/test/net/smtp/test_smtp.rb index 3b9e245..5c0f905 100644 --- a/test/net/smtp/test_smtp.rb +++ b/test/net/smtp/test_smtp.rb @@ -530,6 +530,20 @@ def test_start_instance smtp.finish end + def test_nested_block_start_preserves_active_session + server = FakeServer.start + smtp = Net::SMTP.start("localhost", server.port) + + error = assert_raise(IOError) do + smtp.start { flunk("nested start must not yield") } + end + assert_equal "SMTP session already started", error.message + assert smtp.started? + assert smtp.rset.success? + ensure + smtp.finish if smtp&.started? + end + def test_start_instance_with_position_argument port = fake_server_start(auth: 'plain') smtp = Net::SMTP.new('localhost', port)