title: flask handle thread exceptions
body: <p>I'm trying to display an error if there is an exception in my asynchronous 
   <strong>send_email</strong> function but even after an exception is occurred in the background thread the data still gets stored in the database. The except block never gets executed.
  I'm trying to prevent the database commit and redirect to a page with an error message </p>
  
  <p><strong>send_email function file</strong></p>
  
  <pre><code>from threading import Thread
  from flask import current_app,render_template,flash
  from flask_mail import Message
  from feedback import mail
  
  
  def send_async_email(app, msg):
      try:
          with app.app_context():
              mail.send(msg)
      except:
          raise Exception("Exception occurred in Mail")
          return False
  
  def send_email(to,subject,template,**kwargs):
      app = current_app._get_current_object()
      msg=Message(current_app.config['MAIL_SUBJECT_PREFIX'] + subject,
      sender=current_app.config['MAIL_SENDER'],recipients=[to])
      msg.body=render_template(template +'.txt',**kwargs)
      msg.html=render_template(template +'.html',**kwargs)
      try:
          thr = Thread(target=send_async_email, args=[app, msg])
          thr.start()
          return thr
      except:
          raise Exception("Error occured")
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>@home.route('/register',methods=["GET","POST"])
  def register():
      # form=LoginForm()
      form=LoginForm(prefix="login")
      form1=RegisterForm(prefix="register")
      print(form1.errors)
      if form1.validate_on_submit():
  
          users=Users(email=form1.email.data.strip())
          users.set_password(form1.password.data.strip())
          organization=Organization(organization_name=form1.org_name.data.strip())
          organization.userorg.append(users)
          db.session.add(users)
          db.session.add(organization)
          db.session.flush()
          token=users.generate_confirmation_token()
          try:
              print("INSIDE TRY BLOCK")
              email=send_email(users.email,'Confirm your account','email/confirm_email',user=users,token=token)
          except Exception as e:
              print("INSIDE Exception BLOCK")
              print(e)
              flash("Errors","danger")
              db.session.rollback()
              return redirect(url_for('home.home_view'))
  
          db.session.commit()
          flash("A confirmation email has been sent")
          return redirect(url_for('home.home_view'))
  
  
      return render_template('home/landing-semi.html',form=form,form1=form1)
  </code></pre>
  
  <p><strong>Errors recieved</strong></p>
  
  <pre><code> raise SMTPSenderRefused(code, resp, from_addr)
  smtplib.SMTPSenderRefused: (530, b'5.5.1 Authentication Required. Learn more at\n5.5.1  https://support.google.com/mail/?p=WantAuthError g10-v6sm7818697pfi.148 - gsmtp', '=?utf-8?q?Osfapp_Admin?= &lt;lmnography@gmail.com&gt;')
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "/usr/lib64/python3.6/threading.py", line 916, in _bootstrap_inner
      self.run()
    File "/usr/lib64/python3.6/threading.py", line 864, in run
      self._target(*self._args, **self._kwargs)
    File "/home/leon/Leon/Projects/fmsappv/feedback/modules/utilties.py", line 12, in send_async_email
      raise Exception("Exception occurred in Mail")
  Exception: Exception occurred in Mail
  </code></pre>