question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`io.readLines` yield incomplete lines

See original GitHub issue

Describe the bug

It seems io.readLines yield incomplete lines in some cases.

To Reproduce

Add gen.ts as

import { parse } from "https://deno.land/std@0.105.0/flags/mod.ts";

const opts = parse(Deno.args)
const size = opts.size || 128;
const v = "x".repeat(1024 * size);
const b = new TextEncoder().encode(`["${v}"]`);

await Deno.stdout.write(b);
console.log("");

And add test.ts as

import * as io from "https://deno.land/std@0.100.0/io/mod.ts";

for await (const line of io.readLines(Deno.stdin)) {
  console.log(`Recv ${line.length}`);
  if (!line) {
    continue;
  }
  JSON.parse(line);
}

When the buffer size is small enough, it process successfully.

deno run gen.ts --size=1 | deno run test.ts
Check file:///Users/alisue/ghq/github.com/lambdalisue/deno-io-test/gen.ts
Check file:///Users/alisue/ghq/github.com/lambdalisue/deno-io-test/test.ts
Recv 1028
Recv 0

But when the buffer size is big, io.readLines yield incomplete lines like

deno run gen.ts --size=100 | deno run test.ts
Check file:///Users/alisue/ghq/github.com/lambdalisue/deno-io-test/gen.ts
Check file:///Users/alisue/ghq/github.com/lambdalisue/deno-io-test/test.ts
Recv 16384
error: Uncaught (in promise) SyntaxError: Unexpected end of JSON input
  JSON.parse(line);
       ^
    at JSON.parse (<anonymous>)
    at file:///Users/alisue/ghq/github.com/lambdalisue/deno-io-test/test.ts:8:8

Expected behavior

io.readLines waits for a complete line.

Desktop (please complete the following information):

  • OS: macOS 11.5
  • Version
deno 1.13.0 (release, x86_64-apple-darwin)
v8 9.3.345.11
typescript 4.3.5

Additional context https://github.com/lambdalisue/deno-io-test

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mattncommented, Aug 23, 2021

Sorry, I was cofused. This is not a cause of deno. Please close this,

0reactions
lambdalisuecommented, Aug 23, 2021

write() is a single syscall and it can write up to about 16Kb of data at once (with matches your Recv value). Use writeAll instead and the problem will go away.

Oops. I’ve forgotten about why there is the writeAll function… As you said, I could not find any issue with the following gen.ts

import { parse } from "https://deno.land/std@0.105.0/flags/mod.ts";
import * as io from "https://deno.land/std@0.100.0/io/mod.ts";

const opts = parse(Deno.args)
const size = opts.size || 128;
const v = "x".repeat(1024 * size);
const b = new TextEncoder().encode(`["${v}"]`);

await io.writeAll(Deno.stdout, b);
console.log("");

It can handle 1024 * 1024 data, so it seems it’s no longer an issue for me. Let wait for what @mattn can provide…

Read more comments on GitHub >

github_iconTop Results From Across the Web

TextReader.ReadLine returns incomplete lines - Stack Overflow
I am using a Socket to receive data via TCP, and TextReader.ReadLine to read lines from the connection. There is a problem where...
Read more >
readLines: Read Text Lines from a Connection - Rdrr.io
For a non-blocking text-mode connection the incomplete line is pushed back, silently. For all other connections the line will be accepted, with a...
Read more >
I accidently used `.readlines()`, but why does it look different ...
Read one entire line from the file. A trailing newline character is kept in the string (but may be absent when a file...
Read more >
Class: IO (Ruby 2.5.0)
Reads the entire file specified by name as individual lines, and returns those lines in an array. Lines are separated by sep. a...
Read more >
C#: File.ReadLines() vs File.ReadAllLines() - Medium
ReadAllLines() reads the entire file at once and returns a string[] where each item of the array corresponds to a line of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found