Note that for open file and socket handles it is generally
preferable to set the ``FD_CLOEXEC`` flag (using `fcntl` or
- ``tornado.platform.auto.set_close_exec``) instead
- of using a reload hook to close them.
+ `os.set_inheritable`) instead of using a reload hook to close them.
"""
_reload_hooks.append(fn)
from tornado.concurrent import dummy_executor, run_on_executor
from tornado.ioloop import IOLoop
-from tornado.platform.auto import set_close_exec
from tornado.util import Configurable, errno_from_exception
from typing import List, Callable, Any, Type, Dict, Union, Tuple, Awaitable, Optional
if errno_from_exception(e) == errno.EAFNOSUPPORT:
continue
raise
- set_close_exec(sock.fileno())
if os.name != "nt":
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
`bind_sockets`)
"""
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
- set_close_exec(sock.fileno())
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except socket.error as e:
# but it was closed while still in the accept queue.
# (observed on FreeBSD).
continue
- set_close_exec(connection.fileno())
callback(connection, address)
def remove_handler() -> None:
+++ /dev/null
-#
-# Copyright 2011 Facebook
-#
-# Licensed under the Apache License, Version 2.0 (the "License"); you may
-# not use this file except in compliance with the License. You may obtain
-# a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations
-# under the License.
-
-"""Implementation of platform-specific functionality.
-
-For each function or class described in `tornado.platform.interface`,
-the appropriate platform-specific implementation exists in this module.
-Most code that needs access to this functionality should do e.g.::
-
- from tornado.platform.auto import set_close_exec
-"""
-
-import os
-
-if os.name == "nt":
- from tornado.platform.windows import set_close_exec
-else:
- from tornado.platform.posix import set_close_exec
-
-__all__ = ["set_close_exec"]
+++ /dev/null
-# auto.py is full of patterns mypy doesn't like, so for type checking
-# purposes we replace it with interface.py.
-
-from .interface import *
+++ /dev/null
-#
-# Copyright 2011 Facebook
-#
-# Licensed under the Apache License, Version 2.0 (the "License"); you may
-# not use this file except in compliance with the License. You may obtain
-# a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations
-# under the License.
-
-"""Interfaces for platform-specific functionality.
-
-This module exists primarily for documentation purposes and as base classes
-for other tornado.platform modules. Most code should import the appropriate
-implementation from `tornado.platform.auto`.
-"""
-
-
-def set_close_exec(fd: int) -> None:
- """Sets the close-on-exec bit (``FD_CLOEXEC``)for a file descriptor."""
- raise NotImplementedError()
import os
-def set_close_exec(fd: int) -> None:
- flags = fcntl.fcntl(fd, fcntl.F_GETFD)
- fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
-
-
def _set_nonblocking(fd: int) -> None:
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
+++ /dev/null
-# NOTE: win32 support is currently experimental, and not recommended
-# for production use.
-
-import ctypes
-import ctypes.wintypes
-
-# See: http://msdn.microsoft.com/en-us/library/ms724935(VS.85).aspx
-SetHandleInformation = ctypes.windll.kernel32.SetHandleInformation # type: ignore
-SetHandleInformation.argtypes = (
- ctypes.wintypes.HANDLE,
- ctypes.wintypes.DWORD,
- ctypes.wintypes.DWORD,
-)
-SetHandleInformation.restype = ctypes.wintypes.BOOL
-
-HANDLE_FLAG_INHERIT = 0x00000001
-
-
-def set_close_exec(fd: int) -> None:
- success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, 0)
- if not success:
- raise ctypes.WinError() # type: ignore
from tornado import ioloop
from tornado.iostream import PipeIOStream
from tornado.log import gen_log
-from tornado.platform.auto import set_close_exec
import typing
-from typing import Tuple, Optional, Any, Callable
+from typing import Optional, Any, Callable
if typing.TYPE_CHECKING:
from typing import List # noqa: F401
random.seed(seed)
-def _pipe_cloexec() -> Tuple[int, int]:
- r, w = os.pipe()
- set_close_exec(r)
- set_close_exec(w)
- return r, w
-
-
_task_id = None
pipe_fds = [] # type: List[int]
to_close = [] # type: List[int]
if kwargs.get("stdin") is Subprocess.STREAM:
- in_r, in_w = _pipe_cloexec()
+ in_r, in_w = os.pipe()
kwargs["stdin"] = in_r
pipe_fds.extend((in_r, in_w))
to_close.append(in_r)
self.stdin = PipeIOStream(in_w)
if kwargs.get("stdout") is Subprocess.STREAM:
- out_r, out_w = _pipe_cloexec()
+ out_r, out_w = os.pipe()
kwargs["stdout"] = out_w
pipe_fds.extend((out_r, out_w))
to_close.append(out_w)
self.stdout = PipeIOStream(out_r)
if kwargs.get("stderr") is Subprocess.STREAM:
- err_r, err_w = _pipe_cloexec()
+ err_r, err_w = os.pipe()
kwargs["stderr"] = err_w
pipe_fds.extend((err_r, err_w))
to_close.append(err_w)
from tornado.iostream import IOStream
from tornado import gen
from tornado.netutil import Resolver
-from tornado.platform.auto import set_close_exec
from tornado.gen import TimeoutError
from typing import Any, Union, Dict, Tuple, List, Callable, Iterator, Optional, Set
# - 127.0.0.1 for IPv4
# - ::1 for IPv6
socket_obj = socket.socket(af)
- set_close_exec(socket_obj.fileno())
if source_port_bind or source_ip_bind:
# If the user requires binding also to a specific IP/port.
try:
"tornado.test.util_test",
"tornado.test.web_test",
"tornado.test.websocket_test",
- "tornado.test.windows_test",
"tornado.test.wsgi_test",
]
+++ /dev/null
-import functools
-import os
-import socket
-import unittest
-
-from tornado.platform.auto import set_close_exec
-
-skipIfNonWindows = unittest.skipIf(os.name != "nt", "non-windows platform")
-
-
-@skipIfNonWindows
-class WindowsTest(unittest.TestCase):
- def test_set_close_exec(self):
- # set_close_exec works with sockets.
- s = socket.socket()
- self.addCleanup(s.close)
- set_close_exec(s.fileno())
-
- # But it doesn't work with pipes.
- r, w = os.pipe()
- self.addCleanup(functools.partial(os.close, r))
- self.addCleanup(functools.partial(os.close, w))
- with self.assertRaises(WindowsError):
- set_close_exec(r)