this post was submitted on 03 Sep 2024
859 points (99.5% liked)

Programmer Humor

32453 readers
739 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 94 points 2 months ago (5 children)

bug ocurred randomly.

Fixed it by now btw.

someone's not sharing the actual root cause.

[–] [email protected] 61 points 2 months ago (4 children)

I'm new to Go and wanted to copy some text-data from a stream into the outputstream of the HTTP response. I was copying the data to and from a []byte with a single Read() and Write() call and expexted everything to be copied as the buffer is always the size of the while data. Turns out Read() sometimes fills the whole buffer and sometimes don't.
Now I'm using io.Copy().

[–] [email protected] 51 points 2 months ago (3 children)

Note that this isn't specific to Go. Reading from stream-like data, be it TCP connections, files or whatever always comes with the risk that not all data is present in the local buffer yet. The vast majority of read operations returns the number of bytes that could be read and you should call them in a loop. Same of write operations actually, if you're writing to a stream-like object as the write buffers may be smaller than what you're trying to write.

[–] [email protected] 3 points 2 months ago

Ah yes... several years ago now I was working on a tool called Toxiproxy that (among other things) could slice up the stream chunks into many random small pieces before forwarding them along. It turned out to be very useful for testing applications for this kind of bug.

load more comments (2 replies)
load more comments (2 replies)
load more comments (2 replies)