Releasing file lock
Author: dimagalat
Pull request: yes

`With` scope creates a file lock, which leads to the following error:

INFO:filelock:Lock 1408081097608 released on C:\Users\dimag\.cache\torch\transformers\26bc1ad6c0ac742e9b52263248f6d0f00068293b33709fae12320c0e35ccfbbb.542ce4285a40d23a559526243235df47c5f75c197f04f37d1a0c124c32c9a084.lock
Traceback (most recent call last):
  File "C:\Users\dimag\Anaconda3\envs\pytorch\lib\site-packages\transformers\tokenization_utils.py", line 398, in _from_pretrained
    resume_download=resume_download,
  File "C:\Users\dimag\Anaconda3\envs\pytorch\lib\site-packages\transformers\file_utils.py", line 212, in cached_path
    user_agent=user_agent,
  File "C:\Users\dimag\Anaconda3\envs\pytorch\lib\site-packages\transformers\file_utils.py", line 392, in get_from_cache
    os.rename(temp_file.name, cache_path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\dimag\\.cache\\torch\\transformers\\tmpnhzxze8u' -> 'C:\\Users\\dimag\\.cache\\torch\\transformers\\26bc1ad6c0ac742e9b52263248f6d0f00068293b33709fae12320c0e35ccfbbb.542ce4285a40d23a559526243235df47c5f75c197f04f37d1a0c124c32c9a084'

Comments:
# [Codecov](https://codecov.io/gh/huggingface/transformers/pull/2384?src=pr&el=h1) Report
> Merging [#2384](https://codecov.io/gh/huggingface/transformers/pull/2384?src=pr&el=desc) into [master](https://codecov.io/gh/huggingface/transformers/commit/80faf22b4ac194061a08fde09ad8b202118c151e?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/huggingface/transformers/pull/2384/graphs/tree.svg?width=650&token=9qOlN6Hb1c&height=150&src=pr)](https://codecov.io/gh/huggingface/transformers/pull/2384?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master    #2384   +/-   ##
=======================================
  Coverage   73.24%   73.24%           
=======================================
  Files          87       87           
  Lines       14989    14989           
=======================================
  Hits        10979    10979           
  Misses       4010     4010
```


| [Impacted Files](https://codecov.io/gh/huggingface/transformers/pull/2384?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/transformers/file\_utils.py](https://codecov.io/gh/huggingface/transformers/pull/2384/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9maWxlX3V0aWxzLnB5) | `70.33% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/huggingface/transformers/pull/2384?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/huggingface/transformers/pull/2384?src=pr&el=footer). Last update [80faf22...d0e594f](https://codecov.io/gh/huggingface/transformers/pull/2384?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


@aaugustin could you have a quick look at this PR related to the filelock system?

This isn't directly related to the file lock system. Rather, it's related to moving the file rather than copying it.

Given the current implementation, closing the file before moving it (which is all this PR does) looks safe to me. We're still within the lock-protected section.

Could you take this opportunity remove the following two lines?

```python
                # we are copying the file before closing it, so flush to avoid truncation
                temp_file.flush()
```

Indeed, you're now closing the file before copying it. (To be honest, I should have removed them when I stopped copying the file and started moving it instead.)

@aaugustin I agree, `.flush()` is unnecessary, thanks for pointing it out. I've made the change.

Ok great thanks @dimagalat and @aaugustin