12

I recently setup and deploy an Amazon EC2 instance for deploy my django project.

I was interacting with my application via browser when I get this error in the browser:

errno 5 input/output error django

enter image description here

This error did reference to some function of my application

Environment:

Request Method: GET
Request URL: http://localhost:8000/accounts/profile/

Django Version: 1.9
Python Version: 3.4.3
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'crispy_forms',
 'django_extensions',
 'storages',
 'userprofile']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/home/ubuntu/workspace/neurorehabilitation-system/userprofile/mixins.py" in dispatch
  7.         return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/views/generic/base.py" in get
  157.         context = self.get_context_data(**kwargs)

File "/home/ubuntu/workspace/neurorehabilitation-system/userprofile/views.py" in get_context_data
  50.             print (user.is_physiotherapist)

Exception Type: OSError at /accounts/profile/
Exception Value: [Errno 5] Input/output error

At the end in the line 50 is referenced a get_context_data() function which is inside of a class based view that inherit of TemplateView CBV

but in my console the server require restart and when I did this, the error was solved of a magic way ..

I've search this error and I found this ticket reported https://code.djangoproject.com/ticket/23284

This report is very similar to my error ...

In addition I had this error yesterday, I restart my server, and today I have again the error.

There is some problem with EC2 infraestructure with Django (I don't think so) or the problem is more for my application side?

I don't think so that the function get_context_data() of my application be the problem ...

bgarcial
  • 2,915
  • 10
  • 56
  • 123

3 Answers3

19

I have been exploring, and I should say that the origin of this error were in my code

I have two newbie errors:

  1. print sentence in production

In the traceback that I shown above in my question, I had a print sentence inside my get_context_data() function of this way:

File "/home/ubuntu/workspace/neurorehabilitation-system/userprofile/views.py" in get_context_data
  50.             print (user.is_physiotherapist)

Is possible that each time that this print sentence is executed, the process try write to the stdout file in my amazon ec2 machine instance.

I remove this print sentence in that line, and retrieve the changes into my production servers via git and restart gunicorn server and all it's works perfect.

  1. I have the DEBUG=True in production

I have the following settings files:

settings/
    base.py # --- without DEBUG
    development.py # --- DEBUG=True
    testing.py # --- DEBUG=True
    production.py # --- DEBUG=False
    staging.py # --- DEBUG=False  

All files (development.py, testing.py, production.py, staging.py) inherit from base.py

But I don't know how to make that in my ec2 instance, the production.py be executed, this inherit all from base.py and override DEBUG to False.

I've been exploring and one possibility is change their value (True or False) according to the name of the host in which is running my application, such as shown in this post

In my case this is the value of my hostname

(nrb_dev)ubuntu@ip-172-31-27-249:~$ python
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> a=socket.gethostname()
>>> a
'ip-172-31-27-249'
>>> 
>>> if a != 'ip-172-31-27-249':
...     DEBUG = print ('Caleno juiciocito')
... 
>>> DEBUG
True
>>> 

This mean, put into my base.py the following:

import socket

if socket.gethostname() == 'ip-172-31-27-249':
    DEBUG = False
else:
    DEBUG = True

Although I am hardcoding the hostname of the production server in my code. This mean that I am adding a point thar after will have modified manually when we want deploy my project in other machine with other hostname

Is this a best practice despite to it's works?

Another option which I think that it's the most suited alternative is fix the value of my DJANGO_SETTINGS_MODULE environment variables

In my particular situation I am using virtualenvwrapper and I have two virtual environments so:

nrb_dev for my development environment

nrb_test for my testing environment . I have some hooks which are activated when the virtual environments are activated

In nrb_dev in $VIRTUAL_ENV/bin/postactivate I have this:

export DJANGO_SETTINGS_MODULE="neurorehabilitation.settings.development"

In the same way, in nrb_test in $VIRTUAL_ENV/bin/postactivate I have this:

export DJANGO_SETTINGS_MODULE="neurorehabilitation.settings.testing"

This mean that in my amazon EC2 production machine I should change the hook in $VIRTUAL_ENV/bin/postactivate to choose the settings/production.py of this way:

export DJANGO_SETTINGS_MODULE="neurorehabilitation.settings.production"

Just for test effects and the temporal way, I print the DEBUG value in my settings/production.py

from .base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
print (DEBUG) # just for now.

And when I start my gunicorn daemon server, I can see that DEBUG value is set to False

(nrb_dev)ubuntu@ip-172-31-27-249:~/workspace/neurorehabilitation-system$ gunicorn -c neurorehabilitation/gunicorn_config.py neurorehabilitation.wsgi 
[2016-01-08 00:26:15 +0000] [6691] [INFO] Starting gunicorn 19.4.5
[2016-01-08 00:26:15 +0000] [6691] [INFO] Listening at: http://127.0.0.1:8000 (6691)
[2016-01-08 00:26:15 +0000] [6691] [INFO] Using worker: sync
[2016-01-08 00:26:15 +0000] [6694] [INFO] Booting worker with pid: 6694
False
^C[2016-01-08 00:26:19 +0000] [6691] [INFO] Handling signal: int

Additional Notes

I can explore the Django Logging functionality for register events and others things of my application.

I should explore the supervisor service for manage the gunicorn procees of a better way in production.

Other resources for supervisor:

How to install and manage supervisor in Ubuntu

Setting up Django with Nginx, Gunicorn, virtualenv, supervisor and PostgreSQL

bgarcial
  • 2,915
  • 10
  • 56
  • 123
  • I'm leaving this comment here for someone facing: ```error no 5 input/output error``` in Django. I was making an post request to FCM from django and printing the response on getting a response. This worked fine on local so deployed on dev server. But it worked on dev only when my SSH terminal was open and gave me this error when the SSH terminal was closed. SO I FIXED this issue after removing the print statement and it worked magically. – Abhi Sep 21 '21 at 10:48
1

some possible causes and suggestions for this problem.

Causes:

  1. circular references
  2. hard disk bad sector error
  3. complex process, user, network, permission and other raise mixed error

Suggestions:

  1. use logging replace print, it is best way!!!
  2. redirect IO to a file or to null: python test.py > test.log 2>&1 &, python test.py > /dev/null 2>&1 &
Leo K
  • 5,189
  • 3
  • 12
  • 27
mapleflow
  • 64
  • 1
  • 8
0

File "./my_api/to/Credit.py", line 17, in get_credit_from_response

print xml_response

The stack trace is clear enough. You have a function named get_credit_from_response - inside it the I/O Error is happening. It is probably scheduled every 6/12 hours and causes the issue. Please double check your code.

Community
  • 1
  • 1
masnun
  • 11,635
  • 4
  • 39
  • 50
  • thanks for read and chek the ticket, but this is not mine, I've edit my question for add more details about of my doubt, but when I read your response here, I understood that the ticket reported was one case of error of the application of the user and not of Amazon EC2 in relation to Django. I am right really? I cannot take an screen of my specific error related, but this did reference to one function of my application ... Although as said below I restart my server and all it's solved ... I follow checking my app deployen in EC2 for detail their behavior. :D – bgarcial Jan 06 '16 at 22:22
  • I've edit my question due to the error appear again today. Any orientation will be appreciated. – bgarcial Jan 07 '16 at 15:41
  • I've been exploring and in this post describe a related situation (not with the same error or traceback), but this person also says that he restart your server and all it's solved http://stackoverflow.com/questions/31083103/aws-ec2-t2-micro-instance-very-unstable IN this thread, in one answer, speak about of review the autosacaling section and health checks schedule (may be was useful for their case) , although i think so that my project deployed is too small for this alternative. – bgarcial Jan 07 '16 at 18:47