ramkrishanbhatt / modwsgi

Automatically exported from code.google.com/p/modwsgi
0 stars 0 forks source link

Make mod_wsgi Log object more file like. #82

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Some Python packages, Mercurial being a really good example, rather than do:

  try:
    sys.stdout.flush()
  except:
    pass

when they are worried it may fail, does:

  if not sys.stdout.closed: sys.stdout.flush()

Because mod_wsgi Log object which replaces stdout/stderr isn't fully file like 
and doesn't provide attributes such as 'closed', code like this 
will fail.

The mod_wsgi Log object to counter such preemptive ways of coding things, 
should thus perhaps be made more file like.

Full traceback showing this problem is:

==== Python Traceback ====
{{{
Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/trac/web/main.py", line 419, in _dispatch_request
    dispatcher.dispatch(req)
  File "/usr/lib/python2.5/site-packages/trac/web/main.py", line 172, in dispatch
    chosen_handler)
  File "/usr/lib/python2.5/site-packages/trac/web/main.py", line 285, in _pre_process_request
    chosen_handler = filter_.pre_process_request(req, chosen_handler)
  File "/usr/lib/python2.5/site-packages/trac/versioncontrol/api.py", line 79, in pre_process_request
    self.get_repository(req.authname).sync()
  File "/usr/lib/python2.5/site-packages/trac/versioncontrol/api.py", line 142, in get_repository
    repos = self._connector.get_repository(rtype, rdir, authname)
  File "build/bdist.linux-i686/egg/tracext/hg/backend.py", line 141, in get_repository
    return MercurialRepository(dir, self.log, self.ui, options)
  File "build/bdist.linux-i686/egg/tracext/hg/backend.py", line 219, in __init__
    self.repo = hg.repository(ui=ui, path=path)
  File "/usr/lib/python2.5/site-packages/mercurial/hg.py", line 60, in repository
    repo = _lookup(path).instance(ui, path, create)
  File "/usr/lib/python2.5/site-packages/mercurial/localrepo.py", line 2135, in instance
    return localrepository(ui, util.drop_scheme('file', path), create)
  File "/usr/lib/python2.5/site-packages/mercurial/localrepo.py", line 89, in __init__
    self.ui.readconfig(self.join("hgrc"), self.root)
  File "/usr/lib/python2.5/site-packages/mercurial/ui.py", line 138, in readconfig
    trusted = self._is_trusted(fp, f)
  File "/usr/lib/python2.5/site-packages/mercurial/ui.py", line 125, in _is_trusted
    'user %s, group %s\n') % (f, user, group))
  File "/usr/lib/python2.5/site-packages/mercurial/ui.py", line 445, in warn
    self.write_err(*msg)
  File "/usr/lib/python2.5/site-packages/mercurial/ui.py", line 388, in write_err
    if not sys.stdout.closed: sys.stdout.flush()
AttributeError: 'mod_wsgi.Log' object has no attribute 'closed'

As it turned out, this code was only being triggered because there was a 
mismatch in ownership on repository. That is, WSGI application 
was running as different user to that which owned repository. This was 
therefore triggering error code which did this explicit check rather 
than rely on Python exceptions.

Note that Mercurial has done other odd things in the past such as try and 
automatically determine how it has been executed by doing 
sys.stdin.isatty(), which would also fail under mod_wsgi. Mercurial was doing 
this even though it knew from the outer entry point into it 
that it wasn't being run as an interactive application. They at least fixed 
this issue and when known it was being executed as WSGI 
application, it would self configure itself using that fact to disable its 
interactive ui module.

Original issue reported on code.google.com by Graham.Dumpleton@gmail.com on 30 Apr 2008 at 12:24

GoogleCodeExporter commented 8 years ago
In revision 1060 of trunk for 3.0, added closed and isatty attributes to Log 
object as well as close() method.

For wsgi.errors these aren't required, but log object also used to stderr and 
stdout (when enabled) and code may 
assume these methods may exist for stderr and stdout. The closed and isatty 
attributes always yield false and 
close() will raise an run time error indicating that log cannot be closed.

Original comment by Graham.Dumpleton@gmail.com on 7 Sep 2008 at 4:58

GoogleCodeExporter commented 8 years ago
Also see:

  http://groups.google.com/group/modwsgi/browse_frm/thread/bedd18b56d64fc75?hl=en

for further discussion of issue.

Possible workaround for what Mercurial is doing is to use:

  import sys
  sys.stdout = sys.__stdout__
  sys.stderr = sys.__stderr__

in WSGI script file.

This should only be used if have no other choice though as any logging will now 
no longer appear in Apache error log 
files unless explicitly flushed. Also, if using mod_wsgi daemon mode in a 
virtual host with virtual host specific error log 
file, logging through stdout/stderr will actually appear in main Apache error 
log and not in per virtual host error log file.

Original comment by Graham.Dumpleton@gmail.com on 10 Sep 2008 at 12:15

GoogleCodeExporter commented 8 years ago
Backported to 2.X branch for 2.4 in revision 1239.

Original comment by Graham.Dumpleton@gmail.com on 16 Mar 2009 at 9:58

GoogleCodeExporter commented 8 years ago
Version 2.4 of mod_wsgi now released.

Original comment by Graham.Dumpleton@gmail.com on 11 Apr 2009 at 10:25