Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#29774 closed Bug (fixed)

`django-admin shell` hangs if the user hit enter before shell is ready

Reported by: Craig de Stigter Owned by: Adam Allred
Component: Core (Management commands) Version: dev
Severity: Normal Keywords: hang, shell
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Craig de Stigter)

We have a large project with a fairly slow startup time:

$ time bash -c "echo 'pass' | django-admin shell"

real	0m5.596s
user	0m1.080s
sys	0m1.548s

As a habit, I tend to run django-admin shell, and while it's loading, begin to type my first command.

Since upgrading from django 1.8 to 1.11, this causes the whole process to hang with no output. After about 30s I notice and ctrl+C, but it's incredibly frustrating:

$ django-admin shell
typing while i wait...
^CTraceback (most recent call last):
  File "/kx/dandelion/venv/bin/django-admin", line 11, in <module>
    sys.exit(execute_from_command_line())
  File "/kx/dandelion/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/kx/dandelion/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/kx/dandelion/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/kx/dandelion/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/kx/dandelion/venv/local/lib/python2.7/site-packages/django/core/management/commands/shell.py", line 101, in handle
    exec(sys.stdin.read())
KeyboardInterrupt

Multiple people on our team have experienced this.

It seems to happen if I manage to type at least one line before it loads. If I just type a few characters not including a newline, the hang doesn't occur.

I traced the issue to this addition in the django 1.11 release. It is still present in master.

Change History (7)

comment:1 by Craig de Stigter, 6 years ago

Description: modified (diff)

comment:2 by Tim Graham, 6 years ago

Summary: `django-admin shell` hangs if the user starts typing before shell is ready`django-admin shell` hangs if the user hit enter before shell is ready
Triage Stage: UnreviewedAccepted

In my testing, it seems that hitting "enter" is necessary to reproduce.

comment:3 by Adam Allred, 6 years ago

Owner: changed from nobody to Adam Allred
Status: newassigned

comment:4 by Adam Allred, 6 years ago

Has patch: set

comment:5 by Adam Allred, 6 years ago

Patch at:

https://github.com/django/django/pull/10524

I was a little stumped about how to make a regression test for this, since it's checking for an indefinite block on stdin...but I'm happy to try to implement it if someone could help me over that hurdle.

comment:6 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 4e78e389:

Fixed #29774 -- Fixed django-admin shell hang on startup.

sys.stdin.read() blocks waiting for EOF in shell.py which will
likely never come if the user provides input on stdin via the
keyboard before the shell starts. Added check for a tty to
skip reading stdin if it's not present.

This still allows piping of code into the shell (which should
have no TTY and should have an EOF) but also doesn't cause it
to hang if multi-line input is provided.

comment:7 by Tim Graham <timograham@…>, 6 years ago

In 0d5d8ed3:

[2.1.x] Fixed #29774 -- Fixed django-admin shell hang on startup.

sys.stdin.read() blocks waiting for EOF in shell.py which will
likely never come if the user provides input on stdin via the
keyboard before the shell starts. Added check for a tty to
skip reading stdin if it's not present.

This still allows piping of code into the shell (which should
have no TTY and should have an EOF) but also doesn't cause it
to hang if multi-line input is provided.

Backport of 4e78e389b16fbceb23d5236ee1650b213e71c968 from master.

Note: See TracTickets for help on using tickets.
Back to Top