title: Problems reopening netcdf file written with xarray/dask
body: <p>I am running simulations using dask and store my output results in an xarray Dataset. Then I save the data to a netcdf file. 
  Afterwards, I want to reopen the file for further data processing. Opening the file works, but when I actually access the data (e.g. calling <code>ds.compute()</code>), I get an error which seems to be related to reading in the file. I first thought that is was related to not properly closing the file after writing, but closing it manually does not help. </p>
  
  <p>Strangely, I do not have any problem opening the file and using its data if the data is not produced with dask. If I just store a numpy array with random numbers in the Dataset, everything works fine.</p>
  
  <p>I put together a small example so that you can have a look at it:</p>
  
  <pre><code>import numpy as np
  import xarray as xr
  import pandas as pd
  import dask.array as da
  import dask
  from dask.distributed import Client
  from itertools import repeat
  
  @dask.delayed
  def run_sim(a, b, n_time):
      result = np.array([np.random.randn(n_time)*a,np.random.randn(n_time)+b])
      return result
  
  client = Client()
  
  # Parameters
  n_sims = 5
  n_time = 100
  a_vals = np.random.randn(n_sims)
  b_vals = np.random.randn(n_sims)
  output_file = 'out.nc'
  
  # if I use this as output, computing the data after reopening the file 
  produces an error
  out = da.stack([da.from_delayed(run_sim(*args), (2,n_time,),np.float64) for args in zip(a_vals, b_vals, repeat(n_time))])
  
  # If I use this as output, reopening the netcdf file is no problem
  #out = np.random.randn(n_sims,2,n_time) 
  
  ds = xr.Dataset({'var1': (['realization', 'time'], out[:,0,:]),
                   'var2': (['realization', 'time'],out[:,1,:])},
                   coords={'realization': pd.RangeIndex(n_sims),
                           'time': pd.Index(np.arange(n_time)*.1)})
  
  
  # Save to a netcdf file -&gt; at this point, computations will be carried out
  print('Saving data to netcdf file.')
  ds.to_netcdf(output_file)
  
  # close the netcdf file after writing
  ds.close()
  
  # Reopen the file
  print('Reopen the file.')
  with xr.open_dataset(output_file, chunks={'realization': 2}) as ds:
      # Now acces the data
      ds.compute()
  </code></pre>
  
  <p>The error I get is (I am just copying the last few lines, it is raised when trying to execute <code>ds.compute()</code>):</p>
  
  <pre><code>~/miniconda3/lib/python3.6/site-packages/xarray/backends/netCDF4_.py in _open_netcdf4_group()
      229     import netCDF4 as nc4
      230 
  --&gt; 231     ds = nc4.Dataset(filename, mode=mode, **kwargs)
      232 
      233     with close_on_error(ds):
  
  netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Dataset.__init__()
  
  netCDF4/_netCDF4.pyx in netCDF4._netCDF4._ensure_nc_success()
  
  OSError: [Errno -101] NetCDF: HDF error: b'/home/user/output/out.nc'
  </code></pre>
  
  <p>EDIT: 
  If run the code only until closing the file (<code>ds.close()</code> is the last line executed) and the do a <code>ncdump -h</code> on the output file, I get an error saying that it is unable to open the file ('Resource temporarily unavailable'). So it still seems to be open somewhere. </p>
  
  <p>Running the second part of the code (from <code># Reopen the file</code> on) results in the above cited error.
  But afterwards, <code>ncdump -h</code> on the output file is successful, yielding the expected output:</p>
  
  <pre><code>netcdf out {
  dimensions:
      realization = 5 ;
      time = 100 ;
  variables:
      double var1(realization, time) ;
              var1:_FillValue = NaN ;
      double var2(realization, time) ;
              var2:_FillValue = NaN ;
      int64 realization(realization) ;
      double time(time) ;
              time:_FillValue = NaN ;
  }
  </code></pre>
  
  <p>If I then rerun the last part of the code again I do not get an error anymore. </p>
  
  <p>Am I right in the following conclusions drawn from those results?</p>
  
  <ul>
  <li>The problem arises because the file is not properly closed after writing it. </li>
  <li>The first (unsuccessful) attempt to open the file at least closed it correctly.</li>
  </ul>
  
  <p>If so, what can I do to circumvent this problem?</p>
  
  <p>EDIT2:
  The problem only arises if I start a dask.distributed.Client(). I also opened an <a href="https://github.com/pydata/xarray/issues/2376" rel="nofollow noreferrer">issue on the xarray GitHub page</a>.</p>