Skip to content

Commit

Permalink
fix: Import in macOS (regression from #47) (#49)
Browse files Browse the repository at this point in the history
* fix: Restore importability of fork in Windows
  - The reason of prior failure was an unused ctypes init code.
    We should have removed that code instead. 😉
  • Loading branch information
achimnol committed Apr 12, 2022
1 parent 2c0f9d9 commit 6eb3e39
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 79 deletions.
1 change: 1 addition & 0 deletions changes/49.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix regression of the default imports in macOS by removing the unused code that caused the misleading fix in #47
25 changes: 6 additions & 19 deletions src/aiotools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,38 @@
import sys
import pkgutil

# Import submodules only when installed properly
from . import (
context,
defer as _defer,
fork as _fork,
func,
iter as _iter,
server,
taskgroup,
timer,
)

_is_linux = sys.platform.startswith('linux')

if _is_linux:
from . import (
fork as _fork,
server,
)

__all__ = (
*context.__all__,
*_defer.__all__,
*_fork.__all__,
*func.__all__,
*_iter.__all__,
*server.__all__,
*taskgroup.__all__,
*timer.__all__,
'__version__',
)

from .context import * # noqa
from .defer import * # noqa
from .fork import * # noqa
from .func import * # noqa
from .iter import * # noqa
from .server import * # noqa
from .taskgroup import * # noqa
from .timer import * # noqa

if _is_linux:
__all__ = (
*__all__,
*_fork.__all__,
*server.__all__,
)

from .fork import * # noqa
from .server import * # noqa


_version_data = pkgutil.get_data("aiotools", "VERSION")
if _version_data is None:
Expand Down
60 changes: 0 additions & 60 deletions src/aiotools/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,11 @@
"""

import asyncio
import ctypes
import errno
# import functools
import logging
import os
# import resource
import signal
from abc import ABCMeta, abstractmethod
# from ctypes import (
# CFUNCTYPE,
# byref,
# c_int,
# c_char_p,
# c_void_p,
# cast,
# )
from typing import Callable, Tuple

from .compat import get_running_loop
Expand All @@ -38,10 +27,6 @@

logger = logging.getLogger(__name__)

_libc = ctypes.CDLL(None)
_syscall = _libc.syscall
_default_stack_size = (8 * (2**20)) # 8 MiB

_has_pidfd = False
if hasattr(signal, 'pidfd_send_signal'):
try:
Expand Down Expand Up @@ -262,51 +247,6 @@ async def _clone_pidfd(child_func: Callable[[], int]) -> Tuple[int, int]:
return pid, fd


# The below commneted-out version guarantees the PID reusing issue is prevented
# regardless of SIGCHLD handler configurations.
# However, in complicated real-world applications, it seems to have some
# hard-to-debug side effects when cleaning up... :(

# async def _clone_pidfd(child_func: Callable[[], int]) -> Tuple[int, int]:
# # reference: os_fork_impl() in the CPython source code
# fd = c_int()
# loop = get_running_loop()
#
# # prepare the stack memory
# stack_size = resource.getrlimit(resource.RLIMIT_STACK)[0]
# if stack_size <= 0:
# stack_size = _default_stack_size
# stack = c_char_p(b"\0" * stack_size)
#
# init_pipe = os.pipe()
# init_event = asyncio.Event()
# loop.add_reader(init_pipe[0], init_event.set)
#
# func = CFUNCTYPE(c_int)(
# functools.partial(
# _child_main,
# ctypes.pythonapi.PyOS_AfterFork_Child,
# init_pipe[1],
# child_func,
# )
# )
# stack_top = c_void_p(cast(stack, c_void_p).value + stack_size) # type: ignore
# ctypes.pythonapi.PyOS_BeforeFork()
# # The flag value is CLONE_PIDFD from linux/sched.h
# pid = _libc.clone(func, stack_top, 0x1000, 0, byref(fd))
# ctypes.pythonapi.PyOS_AfterFork_Parent()
#
# # Wait for the child's readiness notification
# await init_event.wait()
# loop.remove_reader(init_pipe[0])
# os.read(init_pipe[0], 1)
# os.close(init_pipe[0])
#
# if pid == -1:
# raise OSError("failed to fork")
# return pid, fd.value


async def afork(child_func: Callable[[], int]) -> AbstractChildProcess:
"""
Fork the current process and execute the given function in the child.
Expand Down

0 comments on commit 6eb3e39

Please sign in to comment.