From 3d19ea4b18fa46bd45b27fa07868c12b96c3494c Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 10 Apr 2013 23:37:22 +0000 Subject: [PATCH] merge file close fix from RC_0_16 --- ChangeLog | 1 + src/file.cpp | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6e0d8de0d..39da328ed 100644 --- a/ChangeLog +++ b/ChangeLog @@ -20,6 +20,7 @@ * fix uTP edge case where udp socket buffer fills up * fix nagle implementation in uTP + * fix hanging issue when closing files on windows (completing a download) * fix piece picking edge case that could cause torrents to get stuck at hash failure * try unencrypted connections first, and fall back to encryption if it fails (performance improvement) * add missing functions to python binding (flush_cache(), remap_files() and orig_files()) diff --git a/src/file.cpp b/src/file.cpp index 9ea4babba..d6f3d3f0e 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -857,7 +857,8 @@ namespace libtorrent } int wait(HANDLE file, error_code& ec) { - if (WaitForSingleObject(ol.hEvent, INFINITE) == WAIT_FAILED) + if (ol.hEvent != INVALID_HANDLE_VALUE + && WaitForSingleObject(ol.hEvent, INFINITE) == WAIT_FAILED) { ec.assign(GetLastError(), get_system_category()); return -1; @@ -999,10 +1000,10 @@ namespace libtorrent DWORD temp; bool use_overlapped = m_open_mode & no_buffer; overlapped_t ol; - ::DeviceIoControl(m_file_handle, FSCTL_SET_SPARSE, 0, 0 + BOOL ret = ::DeviceIoControl(m_file_handle, FSCTL_SET_SPARSE, 0, 0 , 0, 0, &temp, use_overlapped ? &ol.ol : NULL); error_code error; - if (use_overlapped) + if (use_overlapped && ret == FALSE && GetLastError() == ERROR_IO_PENDING) ol.wait(m_file_handle, error); } #else // TORRENT_WINDOWS @@ -1230,7 +1231,7 @@ typedef struct _FILE_ALLOCATED_RANGE_BUFFER { BOOL ret = DeviceIoControl(file, FSCTL_QUERY_ALLOCATED_RANGES, (void*)&in, sizeof(in) , out, sizeof(out), &returned_bytes, overlapped ? &ol.ol : NULL); - if (overlapped) + if (overlapped && ret == FALSE && GetLastError() == ERROR_IO_PENDING) { error_code ec; returned_bytes = ol.wait(file, ec); @@ -1276,10 +1277,10 @@ typedef struct _FILE_ALLOCATED_RANGE_BUFFER { DWORD temp; FILE_SET_SPARSE_BUFFER b; b.SetSparse = FALSE; - int ret = ::DeviceIoControl(m_file_handle, FSCTL_SET_SPARSE, &b, sizeof(b) + BOOL ret = ::DeviceIoControl(m_file_handle, FSCTL_SET_SPARSE, &b, sizeof(b) , 0, 0, &temp, use_overlapped ? &ol.ol : NULL); error_code ec; - if (use_overlapped) + if (use_overlapped && ret == FALSE && GetLastError() == ERROR_IO_PENDING) { ol.wait(m_file_handle, ec); }