Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing to an invalid fd doesn't raise an exception #45763

Closed
tiran opened this issue Nov 11, 2007 · 3 comments
Closed

Writing to an invalid fd doesn't raise an exception #45763

tiran opened this issue Nov 11, 2007 · 3 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@tiran
Copy link
Member

tiran commented Nov 11, 2007

BPO 1422
Nosy @gvanrossum, @tiran

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/tiran'
closed_at = <Date 2007-11-12.16:58:42.070>
created_at = <Date 2007-11-11.17:26:36.341>
labels = ['interpreter-core', 'type-bug']
title = "Writing to an invalid fd doesn't raise an exception"
updated_at = <Date 2008-01-06.22:29:45.174>
user = 'https://github.com/tiran'

bugs.python.org fields:

activity = <Date 2008-01-06.22:29:45.174>
actor = 'admin'
assignee = 'christian.heimes'
closed = True
closed_date = <Date 2007-11-12.16:58:42.070>
closer = 'gvanrossum'
components = ['Interpreter Core']
creation = <Date 2007-11-11.17:26:36.341>
creator = 'christian.heimes'
dependencies = []
files = []
hgrepos = []
issue_num = 1422
keywords = []
message_count = 3.0
messages = ['57372', '57376', '57401']
nosy_count = 2.0
nosy_names = ['gvanrossum', 'christian.heimes']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue1422'
versions = ['Python 2.6', 'Python 2.5', 'Python 3.0']

@tiran
Copy link
Member Author

tiran commented Nov 11, 2007

The bug is related to http://bugs.python.org/issue1415 and occurs only
with the latest patch from bpo-1415.

Writing to an invalid fd doesn't raise an exception:

>>> f = open(100, 'w')
>>> f.fileno()
100
>>> f.write("test")
4

However reading or opening an invalid fd for reading and writing raises
an exception.

>>> f = open(100, 'r')
>>> f.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/heimes/dev/python/py3k/Lib/io.py", line 1253, in read
    res += decoder.decode(self.buffer.read(), True)
  File "/home/heimes/dev/python/py3k/Lib/io.py", line 756, in read
    current = self.raw.read(to_read)
IOError: [Errno 9] Bad file descriptor
>>> f = open(100, 'w+')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/heimes/dev/python/py3k/Lib/io.py", line 195, in __new__
    return open(*args, **kwargs)
  File "/home/heimes/dev/python/py3k/Lib/io.py", line 169, in open
    buffer = BufferedRandom(raw, buffering)
  File "/home/heimes/dev/python/py3k/Lib/io.py", line 948, in __init__
    raw._checkSeekable()
  File "/home/heimes/dev/python/py3k/Lib/io.py", line 301, in _checkSeekable
    if msg is None else msg)
IOError: File or stream is not seekable.

I expected that fileio_write() raises an exception when fd is invalid:

        n = write(self->fd, ptr, n);
        if (n < 0) {
                if (errno == EAGAIN)
                        Py_RETURN_NONE;
                PyErr_SetFromErrno(PyExc_IOError);
                return NULL;
        }

@tiran tiran self-assigned this Nov 11, 2007
@tiran tiran added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Nov 11, 2007
@tiran
Copy link
Member Author

tiran commented Nov 11, 2007

Python 2.5 and probably 2.6 suffer from the same problem although it's
harder to trigger it.

Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> f = open("/tmp/test", 'w')
>>> f.fileno()
3
>>> os.close(f.fileno())
>>> f.write("test")
>>> f.write("test")
>>>
close failed: [Errno 9] Bad file descriptor
$ ls -la /tmp/test
-rw-r----- 1 heimes heimes 0 2007-11-11 20:46 /tmp/test

@gvanrossum
Copy link
Member

I don't think this is worth fixing; you'll get an I/O error as soon as
I/O is attempted, which is upon the first read() for input, or upon the
firsh (implicit or explicit) flush() on output. You can't tell whether
a fd is valid or not without attempting I/O on it, so trying to test on
each write() call would defeat the purpose of buffering. Testing on
open() is insufficient as long as anybody could call os.close() any time.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants