[ruby-core:121608] [Ruby Bug#21260] duping stringio objects shares cursors

Issue #21260 has been reported by chucke (Tiago Cardoso). ---------------------------------------- Bug #21260: duping stringio objects shares cursors https://bugs.ruby-lang.org/issues/21260 * Author: chucke (Tiago Cardoso) * Status: Open * ruby -v: ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +PRISM [x86_64-darwin23] * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- I've found that, if I `.dup` a stringio, when I read one of the objects, the cursor also moves internally on the other: ```ruby require "stringio" s1 = StringIO.new("test") s2 = s1.dup s1.read #=> "test" s2.read #=> "" s1.rewind s2.rewind s2.read #=> "test" s1.read #=> "" s1.pos #=> 4 s2.pos #=> 4 ``` -- https://bugs.ruby-lang.org/

Issue #21260 has been updated by jeremyevans0 (Jeremy Evans). This is how `IO#dup` works. What would be the benefit of `StringIO#dup` behaving differently? ```ruby f = File.open('filename.rb') f2 = f.dup f2.pos # => 0 f.read 4 f2.pos # => 4 ``` ---------------------------------------- Bug #21260: duping stringio objects shares cursors https://bugs.ruby-lang.org/issues/21260#change-112669 * Author: chucke (Tiago Cardoso) * Status: Open * ruby -v: ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +PRISM [x86_64-darwin23] * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- I've found that, if I `.dup` a stringio, when I read one of the objects, the cursor also moves internally on the other: ```ruby require "stringio" s1 = StringIO.new("test") s2 = s1.dup s1.read #=> "test" s2.read #=> "" s1.rewind s2.rewind s2.read #=> "test" s1.read #=> "" s1.pos #=> 4 s2.pos #=> 4 ``` -- https://bugs.ruby-lang.org/

Issue #21260 has been updated by chucke (Tiago Cardoso). I see. Alignment with File makes sense. At least my expectation was that, by dup'ing the IO object, I'd receive a separate independent object with which to traverse the file. But perhaps that does not make sense, as at least in the case of File, that'd mean a second OS file descriptor, and that's heavy? Also not sure whether this expectation is general. ---------------------------------------- Bug #21260: duping stringio objects shares cursors https://bugs.ruby-lang.org/issues/21260#change-112672 * Author: chucke (Tiago Cardoso) * Status: Feedback * ruby -v: ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +PRISM [x86_64-darwin23] * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- I've found that, if I `.dup` a stringio, when I read one of the objects, the cursor also moves internally on the other: ```ruby require "stringio" s1 = StringIO.new("test") s2 = s1.dup s1.read #=> "test" s2.read #=> "" s1.rewind s2.rewind s2.read #=> "test" s1.read #=> "" s1.pos #=> 4 s2.pos #=> 4 ``` -- https://bugs.ruby-lang.org/
participants (2)
-
chucke (Tiago Cardoso)
-
jeremyevans0 (Jeremy Evans)