diff --git a/lib/gitlab/ci/trace/stream.rb b/lib/gitlab/ci/trace/stream.rb
index 33141d0d88d0daae71231e7d2ed0aed50d67a617..8d9fb6ff0f1e99e1ab6b0c6e8aa540838b795b8c 100644
--- a/lib/gitlab/ci/trace/stream.rb
+++ b/lib/gitlab/ci/trace/stream.rb
@@ -15,6 +15,8 @@ module Gitlab
         def initialize
           @stream = yield
           @stream.binmode
+          # Ci::Ansi2html::Converter would read from @stream directly
+          @stream.set_encoding(Encoding.default_external)
         end
 
         def valid?
diff --git a/spec/lib/gitlab/ci/trace/stream_spec.rb b/spec/lib/gitlab/ci/trace/stream_spec.rb
index 4bbca6d2ea23cf858b6c891b9b0c828d6f9b2c74..6f5c9994f541ea4915bafe08b13ba9fb6d75889d 100644
--- a/spec/lib/gitlab/ci/trace/stream_spec.rb
+++ b/spec/lib/gitlab/ci/trace/stream_spec.rb
@@ -34,12 +34,12 @@ describe Gitlab::Ci::Trace::Stream do
     end
 
     context 'when the trace contains ANSI sequence and Unicode' do
-      let(:stream) do
-        described_class.new do
-          File.open(expand_fixture_path('trace/ansi-sequence-and-unicode'))
-        end
+      let(:io) do
+        File.open(expand_fixture_path('trace/ansi-sequence-and-unicode'))
       end
 
+      let(:stream) { described_class.new { io } }
+
       it 'forwards to the next linefeed, case 1' do
         stream.limit(7)
 
@@ -57,6 +57,16 @@ describe Gitlab::Ci::Trace::Stream do
         expect(result).to eq("\e[01;32m許功蓋\e[0m\n")
         expect(result.encoding).to eq(Encoding.default_external)
       end
+
+      # See https://gitlab.com/gitlab-org/gitlab-ce/issues/30796
+      it 'reads in binary, output as Encoding.default_external' do
+        stream.limit(29)
+
+        result = io.read # Ci::Ansi2html::Converter would read with each_line
+
+        expect(result).to eq("\e[01;32m許功蓋\e[0m\n")
+        expect(result.encoding).to eq(Encoding.default_external)
+      end
     end
   end