message: Add a try/finally to context managers.
  This way the state is always reset if an error occurs after the context manager yields.
diff: @@ -74,9 +74,11 @@ def record_summaries_every_n_global_steps(n, global_step=None):
  global_step = training_util.get_or_create_global_step()
  collection_ref = ops.get_collection_ref(_SHOULD_RECORD_SUMMARIES_NAME)
  old = collection_ref[:]
  + try:
  with ops.device("cpu:0"):
  collection_ref[:] = [math_ops.equal(global_step % n, 0)]
  yield
  + finally:
  collection_ref[:] = old
  @@ -85,8 +87,10 @@ def always_record_summaries():
  """Sets the should_record_summaries Tensor to always true."""
  collection_ref = ops.get_collection_ref(_SHOULD_RECORD_SUMMARIES_NAME)
  old = collection_ref[:]
  + try:
  collection_ref[:] = [True]
  yield
  + finally:
  collection_ref[:] = old
  @@ -95,8 +99,10 @@ def never_record_summaries():
  """Sets the should_record_summaries Tensor to always false."""
  collection_ref = ops.get_collection_ref(_SHOULD_RECORD_SUMMARIES_NAME)
  old = collection_ref[:]
  + try:
  collection_ref[:] = [False]
  yield
  + finally:
  collection_ref[:] = old
  @@ -127,13 +133,17 @@ class SummaryWriter(object):
  yield self
  else:
  old = context.context().summary_writer_resource
  + try:
  context.context().summary_writer_resource = self._resource
  yield self
  - # Flushes the summary writer in eager mode or in graph functions, but not
  - # in legacy graph mode (you're on your own there).
  - self.flush()
  + # Flushes the summary writer in eager mode or in graph functions, but
  + # not in legacy graph mode (you're on your own there).
  + with ops.device("cpu:0"):
  + gen_summary_ops.flush_summary_writer(self._resource)
  + finally:
  context.context().summary_writer_resource = old
  +
  def init(self):
  """Operation to initialize the summary writer resource."""
  if self._resource is not None:
  