You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a simplified repro from our code base. The following code finishes execution and outputs the string. The code worked fine till 3.1-preview1. As upgraded to 3.1-preview2 it started to hang.
The hang is in the step where we call decoder.Convert. After a few iterations, it falls in a state where the all the bytes are read but the completed variable is never set to true and hence it keeps on looping.
Source code:
I have attached the Utf8.txt file here which i try to read.
using System;using System.Text;using System.IO;namespacetest{classProgram{staticvoidMain(string[]args){
Console.WriteLine("Hello World!");using(Streamstream= File.Open(@"d:\temp\Utf8.txt", FileMode.Open)){
Console.WriteLine(StreamToString(stream, Encoding.UTF8));}}privatestaticstringStreamToString(Streamstream,Encodingencoding){StringBuilderresult=new StringBuilder(capacity:10000);Decoderdecoder= encoding.GetDecoder();intuseBufferSize=64;if(useBufferSize< encoding.GetMaxCharCount(10)){useBufferSize= encoding.GetMaxCharCount(10);}char[]chars=newchar[useBufferSize];byte[]bytes=newbyte[useBufferSize*4];intbytesRead=0;do{// Read at most the number of bytes that will fit in the input buffer. The// return value is the actual number of bytes read, or zero if no bytes remain.bytesRead= stream.Read(bytes,0,useBufferSize*4);boolcompleted=false;intbyteIndex=0;intbytesUsed;intcharsUsed;while(!completed){// If this is the last input data, flush the decoder's internal buffer and state.boolflush=(bytesRead==0);
decoder.Convert(bytes, byteIndex,bytesRead-byteIndex,
chars,0, useBufferSize, flush,out bytesUsed,out charsUsed,out completed);// The conversion produced the number of characters indicated by charsUsed. Write that number// of characters to our result buffer
result.Append(chars,0, charsUsed);// Increment byteIndex to the next block of bytes in the input buffer, if any, to convert.byteIndex+=bytesUsed;}}while(bytesRead!=0);return result.ToString();}}}
Output:
With 3.1-preview.1
PS D:\code\test> dotnet run
Hello World!
<h1>Unicode Demo</h1>
<p>Taken from <a
href="http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-demo.txt">http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-demo.txt</a></p>
<pre>
║ ║
║ ASCII safety test: 1lI|, 0OD, 8B ║
║ ?─────────? ║
║ the euro symbol: │ 14.95 ? │ ║
║ ?─────────? ║
╚══════════════════════════════════════════╝
</pre>
Followed up with Aditya offline. (Thanks btw for the excellent repro!) This behavior is a consequence of dotnet/coreclr#27229 and is expected, since now that completed is correctly returning false to indicate that the operation is not complete the innermost loop never terminates.
all input data has been converted and stored in the destination buffer, and
there's no leftover state in the Decoder instance that requires further processing.
My recommendation would be to change the innermost loop so that it only checks the completed parameter when flush = true. If flush = false and you know you're going to call the Decoder.Convert method again later, only check that all input bytes have been consumed. It's acceptable for the Decoder instance to have remaining internal state in this case.
This is a simplified repro from our code base. The following code finishes execution and outputs the string. The code worked fine till
3.1-preview1
. As upgraded to3.1-preview2
it started to hang.The hang is in the step where we call
decoder.Convert
. After a few iterations, it falls in a state where the all the bytes are read but thecompleted
variable is never set to true and hence it keeps on looping.Source code:
I have attached the Utf8.txt file here which i try to read.
Output:
With 3.1-preview.1
With 3.1-preview.2
The text was updated successfully, but these errors were encountered: