{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/corefx/issues/31056","repository_url":"https://api.github.com/repos/dotnet/corefx","labels_url":"https://api.github.com/repos/dotnet/corefx/issues/31056/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/31056/comments","events_url":"https://api.github.com/repos/dotnet/corefx/issues/31056/events","html_url":"https://github.com/dotnet/corefx/issues/31056","id":341110166,"node_id":"MDU6SXNzdWUzNDExMTAxNjY=","number":31056,"title":"Task.Delay(-1) should not create a leak","user":{"login":"NicolasDorier","id":3020646,"node_id":"MDQ6VXNlcjMwMjA2NDY=","avatar_url":"https://avatars0.githubusercontent.com/u/3020646?v=4","gravatar_id":"","url":"https://api.github.com/users/NicolasDorier","html_url":"https://github.com/NicolasDorier","followers_url":"https://api.github.com/users/NicolasDorier/followers","following_url":"https://api.github.com/users/NicolasDorier/following{/other_user}","gists_url":"https://api.github.com/users/NicolasDorier/gists{/gist_id}","starred_url":"https://api.github.com/users/NicolasDorier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NicolasDorier/subscriptions","organizations_url":"https://api.github.com/users/NicolasDorier/orgs","repos_url":"https://api.github.com/users/NicolasDorier/repos","events_url":"https://api.github.com/users/NicolasDorier/events{/privacy}","received_events_url":"https://api.github.com/users/NicolasDorier/received_events","type":"User","site_admin":false},"labels":[{"id":881802182,"node_id":"MDU6TGFiZWw4ODE4MDIxODI=","url":"https://api.github.com/repos/dotnet/corefx/labels/area-System.Threading.Tasks","name":"area-System.Threading.Tasks","color":"d4c5f9","default":false},{"id":309869280,"node_id":"MDU6TGFiZWwzMDk4NjkyODA=","url":"https://api.github.com/repos/dotnet/corefx/labels/question","name":"question","color":"207de5","default":true}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/dotnet/corefx/milestones/5","html_url":"https://github.com/dotnet/corefx/milestone/5","labels_url":"https://api.github.com/repos/dotnet/corefx/milestones/5/labels","id":1328080,"node_id":"MDk6TWlsZXN0b25lMTMyODA4MA==","number":5,"title":"Future","description":"Milestone tracking issues not triaged to a planned release. Work in this milestone may happen, or may not: it is not committed.","creator":{"login":"joshfree","id":1302850,"node_id":"MDQ6VXNlcjEzMDI4NTA=","avatar_url":"https://avatars1.githubusercontent.com/u/1302850?v=4","gravatar_id":"","url":"https://api.github.com/users/joshfree","html_url":"https://github.com/joshfree","followers_url":"https://api.github.com/users/joshfree/followers","following_url":"https://api.github.com/users/joshfree/following{/other_user}","gists_url":"https://api.github.com/users/joshfree/gists{/gist_id}","starred_url":"https://api.github.com/users/joshfree/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joshfree/subscriptions","organizations_url":"https://api.github.com/users/joshfree/orgs","repos_url":"https://api.github.com/users/joshfree/repos","events_url":"https://api.github.com/users/joshfree/events{/privacy}","received_events_url":"https://api.github.com/users/joshfree/received_events","type":"User","site_admin":false},"open_issues":1914,"closed_issues":229,"state":"open","created_at":"2015-09-29T00:25:45Z","updated_at":"2018-07-13T23:17:14Z","due_on":null,"closed_at":null},"comments":1,"created_at":"2018-07-13T18:05:13Z","updated_at":"2018-07-13T23:17:14Z","closed_at":"2018-07-13T23:17:14Z","author_association":"NONE","body":"# Issue\r\n\r\n\r\nI implemented this so I could make any Task cancellable:\r\n\r\n```c#\r\npublic static async Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken)\r\n{\r\n            var waiting = Task.Delay(-1, delayCTS.Token);\r\n            var doing = task;\r\n            await Task.WhenAny(waiting, doing);\r\n            cancellationToken.ThrowIfCancellationRequested();\r\n            return await doing;\r\n}\r\n```\r\n\r\nBut to my surprise, it created a leak! As `Task.Delay` get added to a list.\r\n\r\n![image](https://user-images.githubusercontent.com/3020646/42706696-a9d827aa-8712-11e8-8f92-3f61958a57e0.png)\r\n\r\n# Workaround\r\n```c#\r\npublic static async Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken)\r\n{\r\n        using (var delayCTS = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))\r\n        {\r\n            var waiting = Task.Delay(-1, delayCTS.Token);\r\n            var doing = task;\r\n            await Task.WhenAny(waiting, doing);\r\n            delayCTS.Cancel();\r\n            cancellationToken.ThrowIfCancellationRequested();\r\n            return await doing;\r\n        }\r\n}\r\n```\r\n\r\nThere is no reason `Task.Delay(-1)`  get monitored.\r\n\r\n# Expected result\r\n\r\nThis code should not leak\r\n```c#\r\npublic static async Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken)\r\n{\r\n            var waiting = Task.Delay(-1, delayCTS.Token);\r\n            var doing = task;\r\n            await Task.WhenAny(waiting, doing);\r\n            cancellationToken.ThrowIfCancellationRequested();\r\n            return await doing;\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/dotnet/corefx/issues/comments/404976750","html_url":"https://github.com/dotnet/corefx/issues/31056#issuecomment-404976750","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/31056","id":404976750,"node_id":"MDEyOklzc3VlQ29tbWVudDQwNDk3Njc1MA==","user":{"login":"tarekgh","id":10833894,"node_id":"MDQ6VXNlcjEwODMzODk0","avatar_url":"https://avatars2.githubusercontent.com/u/10833894?v=4","gravatar_id":"","url":"https://api.github.com/users/tarekgh","html_url":"https://github.com/tarekgh","followers_url":"https://api.github.com/users/tarekgh/followers","following_url":"https://api.github.com/users/tarekgh/following{/other_user}","gists_url":"https://api.github.com/users/tarekgh/gists{/gist_id}","starred_url":"https://api.github.com/users/tarekgh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tarekgh/subscriptions","organizations_url":"https://api.github.com/users/tarekgh/orgs","repos_url":"https://api.github.com/users/tarekgh/repos","events_url":"https://api.github.com/users/tarekgh/events{/privacy}","received_events_url":"https://api.github.com/users/tarekgh/received_events","type":"User","site_admin":false},"created_at":"2018-07-13T23:17:14Z","updated_at":"2018-07-13T23:17:14Z","author_association":"MEMBER","body":"@NicolasDorier I am closing this as this is expected behavior as @stephentoub mentioned. feel free to reply with any more questions if you have any."}}
{"actor":{"display_login":"tarekgh"}}