Why does reading from file descriptor 3 hangs with bash and network redirection?

I am using bash and network redirection to send HTTP requests.

Here is the request I am sending:

exec 3<> $initial_url
path="/webhdfs/v1$hdfs_target?user.name=hdfs&op=OPEN"
printf "GET $path HTTP/1.1\r\nHost: $url:$port\r\nAccept: */*\r\n\r\n" >&3
echo toto
cat <&3
echo titi
exec 3<&

I am recieving the response correctly. However, the cat just hangs indefinitely. Here is an execution output (killed with ctrl+c):

$ time bash toto.sh
toto
HTTP/1.1 307 TEMPORARY_REDIRECT
Cache-Control: no-cache
Expires: Thu, 20 Jul 2023 12:29:41 GMT
Date: Thu, 20 Jul 2023 12:29:41 GMT
Pragma: no-cache
Expires: Thu, 20 Jul 2023 12:29:41 GMT
Date: Thu, 20 Jul 2023 12:29:41 GMT
Pragma: no-cache
X-FRAME-OPTIONS: SAMEORIGIN
Set-Cookie: hadoop.auth="redacted_for_SO"; Path=/; HttpOnly
Location: redacted_for_SO
Content-Type: application/octet-stream
Content-Length: 0



real    0m19.042s
user    0m0.124s
sys     0m0.733s

There is everything I need in this output, but as said, the cat never ends.

  • Why is the cat hanging ? What is it waiting for ?
  • How to prevent it from hanging when the data has been recieved ?

>Solution :

You should add a Connection: close header to your request so that the server will close the connection once it’s finished sending the response. See also here: https://serverfault.com/questions/790197/what-does-connection-close-mean-when-used-in-the-response-message

Leave a Reply