/ proc is a virtual file system . This means that reading from "files" in it does not match reading from a regular file system.
If the process terminates, information about this is deleted from / proc much faster than if it were a real file system (the dirty cache reset delay was involved here).
Given this, imagine that the process ends before you read the next line, which has not yet been buffered.
The solution is to take into account the loss of the file, as you may not need information about a process that no longer exists, or buffer the entire file and then analyze it.
EDIT: The hang in the process should be explicitly related to the fact that it is a virtual file system. It does not behave exactly like a real file system. Since this is a specific type of fs, the problem may be in the fs driver. The code you provide is great for reading files normally.
source share