# Buggy code
def open_closed_files(self):
    """Open some closed files if they may have new lines.

    Opening more files may require us to close some of the already open
    files.
    """
    if not self.can_open_more_files:
        # If we can't open any more files. Close all of the files.
        self.close_all_files()

    files_with_no_updates = []
    while len(self.closed_file_infos) > 0:
        if len(self.open_file_infos) >= ray_constants.LOG_MONITOR_MAX_OPEN_FILES:
            self.can_open_more_files = False
            break

        file_info = self.closed_file_infos.pop(0)
        assert file_info.file_handle is None
        # Get the file size to see if it has gotten bigger since we last
        # opened it.
        try:
            file_size = os.path.getsize(file_info.filename)
        except (IOError, OSError) as e:
            # Catch "file not found" errors.
            if e.errno == errno.ENOENT:
                logger.warning(
                    "Warning: The file {} was not found.".format(file_info.filename)
                )
                self.log_filenames.remove(file_info.filename)
                continue
            raise e

        # If some new lines have been added to this file, try to reopen the
        # file.
        if file_size > file_info.size_when_last_opened:
            try:
                f = open(file_info.filename, "r")
            except (IOError, OSError) as e:
                if e.errno == errno.ENOENT:
                    logger.warning(
                        "Warning: The file {} was not found.".format(file_info.filename)
                    )
                    self.log_filenames.remove(file_info.filename)
                    continue
                else:
                    raise e

            f.seek(file_info.file_position)
            file_info.filesize_when_last_opened = file_size
            file_info.file_handle = f
            self.open_file_infos.append(file_info)
        else:
            files_with_no_updates.append(file_info)

    # Add the files with no changes back to the list of closed files.
    self.closed_file_infos += files_with_no_updates


# Traceback (UnicodeDecodeError)
2019-03-15 15:48:49,381	ERROR worker.py:1752 -- The log monitor on node ip-10-2-247-245 failed with the following error:
Traceback (most recent call last):
File "/home/ubuntu/longshotsyndicate/ray/python/ray/log_monitor.py", line 268, in <module>
log_monitor.run()
File "/home/ubuntu/longshotsyndicate/ray/python/ray/log_monitor.py", line 219, in run
anything_published = self.check_log_files_and_publish_updates()
File "/home/ubuntu/longshotsyndicate/ray/python/ray/log_monitor.py", line 175, in check_log_files_and_publish_updates
next_line = file_info.file_handle.readline()
File "/home/ubuntu/conda/envs/venv/lib/python3.7/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc2 in position 0: unexpected end of data

# Fixed code
def open_closed_files(self):
    """Open some closed files if they may have new lines.

    Opening more files may require us to close some of the already open
    files.
    """
    if not self.can_open_more_files:
        # If we can't open any more files. Close all of the files.
        self.close_all_files()

    files_with_no_updates = []
    while len(self.closed_file_infos) > 0:
        if len(self.open_file_infos) >= ray_constants.LOG_MONITOR_MAX_OPEN_FILES:
            self.can_open_more_files = False
            break

        file_info = self.closed_file_infos.pop(0)
        assert file_info.file_handle is None
        # Get the file size to see if it has gotten bigger since we last
        # opened it.
        try:
            file_size = os.path.getsize(file_info.filename)
        except (IOError, OSError) as e:
            # Catch "file not found" errors.
            if e.errno == errno.ENOENT:
                logger.warning(
                    "Warning: The file {} was not found.".format(file_info.filename)
                )
                self.log_filenames.remove(file_info.filename)
                continue
            raise e

        # If some new lines have been added to this file, try to reopen the
        # file.
        if file_size > file_info.size_when_last_opened:
            try:
                f = open(file_info.filename, "rb")
            except (IOError, OSError) as e:
                if e.errno == errno.ENOENT:
                    logger.warning(
                        "Warning: The file {} was not found.".format(file_info.filename)
                    )
                    self.log_filenames.remove(file_info.filename)
                    continue
                else:
                    raise e

            f.seek(file_info.file_position)
            file_info.filesize_when_last_opened = file_size
            file_info.file_handle = f
            self.open_file_infos.append(file_info)
        else:
            files_with_no_updates.append(file_info)

    # Add the files with no changes back to the list of closed files.
    self.closed_file_infos += files_with_no_updates