-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract pty class and improve cleanup
- Loading branch information
Showing
4 changed files
with
78 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import asyncio | ||
import logging | ||
from pty import openpty | ||
|
||
import aiofiles | ||
|
||
from ..util.async_files import chown, close | ||
from ..util.async_helpers import timeout | ||
from ..util.terminal import set_winsize | ||
from ..util.user_config import get_gid, get_uid | ||
|
||
|
||
class Pty: | ||
@classmethod | ||
async def create(cls, user): | ||
self = cls() | ||
self.front_fd, self.back_fd = openpty() | ||
|
||
# on some distros process user must own 'back', otherwise you get: | ||
# cannot set terminal process group (-1): Inappropriate ioctl for device | ||
await chown(self.back_fd, get_uid(user), get_gid(user)) | ||
|
||
self.front = await aiofiles.open(self.front_fd, "w+b", 0) | ||
self.back = await aiofiles.open(self.back_fd, "r+b", 0) | ||
|
||
return self | ||
|
||
def set_winsize(self, rows, cols): | ||
set_winsize(self.back_fd, 4, 60) | ||
|
||
async def write(self, content_bytes): | ||
await self.front.write(content_bytes) | ||
|
||
async def clean_up(self): | ||
logging.exception("PTY Closing master") | ||
await self._clean_up_end(self.front, self.front_fd) | ||
logging.exception("PTY Closing slave") | ||
await self._clean_up_end(self.back, self.back_fd) | ||
logging.exception("PTY Cleanup complete") | ||
|
||
async def _clean_up_end(self, file, fd): | ||
# aiofiles close sometimes hangs, so use a timeout and try closing the | ||
# fd directly | ||
async def close_end(): | ||
try: | ||
logging.debug("PTY Closing file") | ||
await file.close() | ||
except Exception as e: | ||
logging.exception(f"PTY Close error: {e}") | ||
|
||
close_task = asyncio.create_task(close_end()) | ||
done = await timeout(close_task, 0.1) | ||
if close_task not in done: | ||
logging.debug("PTY Close timed out") | ||
|
||
try: | ||
logging.debug("PTY Closing fd") | ||
await close(fd) | ||
except Exception as e: | ||
logging.exception(f"PTY fd close error: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters