locked
Unable to cancel StreamSocket read operation

    Question

  • Consider this code:

    	 task<void>(create_async([]() {		
    		auto s = ref new StreamSocket();
    
    		/*
    		 * Connect to host
    		 */
    		task<void>(s->ConnectAsync(ref new HostName(ref new Platform::String(L"google.com")), ref new Platform::String(L"80"))).get();
    
    		auto reader = ref new DataReader(s->InputStream);
    
    		cancellation_token_source read_operation;
    
    		WRL::Wrappers::Event evt(CreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS));
    
    		/*
    		 * Start reading
    		 */
    		task<unsigned int>(reader->LoadAsync(100), read_operation.get_token()).then([&evt](task<unsigned int> t){
    			try
    			{
    				unsigned int num_read = t.get();				
    			}			
    			catch (...)
    			{
    			}
    			SetEvent(evt.Get());
    		});
    		
    		WaitForSingleObjectEx(evt.Get(), 2000, FALSE);
    
    		/*
    		 * Cancel the operation after 2 seconds of wait
    		 */
    		read_operation.cancel();
    
    		/*
    		 * Wait for continuation to be called
    		 */
    		WaitForSingleObjectEx(evt.Get(), INFINITE, FALSE);
    	})).then([](task<void> t) {});

    This code initiates a read operation and after 2 seconds cancels it, however the continuation is never called and no task_canceled exception is being thrown, the code just hangs forever in 

    WaitForSingleObjectEx(evt.Get(), INFINITE, FALSE);

    Is this a bug ? If yes, how to workaround ?

    Tuesday, March 06, 2012 9:33 AM

Answers

  • Hi Stephph,

    Thanks for reporting this. I created another bug for this issue as well. Keep the feedback coming!


    David Lamb

    • Marked as answer by Shephph Wednesday, March 07, 2012 11:43 AM
    Wednesday, March 07, 2012 4:48 AM
    Moderator