Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp978453pxb; Sat, 30 Oct 2021 03:07:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxWxwmKj5g5LxdipXyvWSOBxuVB6WtxYvvCnXeSL87r7cX+1LkWD+VQVO7MwtMrIQoI91+3 X-Received: by 2002:a92:cd8f:: with SMTP id r15mr5531003ilb.278.1635588475747; Sat, 30 Oct 2021 03:07:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635588475; cv=none; d=google.com; s=arc-20160816; b=ylP0SzCRNgGOWmaHy1S5Ewx0PNK6tcnO0KcNMV1fIHdYb2Bg+5rXrhMU5YfFSokfSM YzCid/2KVc1ayfRKP/x9Q2mAZ10UBBfjl6K/x4YEicghMb7xZmgnjrTkd9BOlYPCbPqh p/eobn5GhU+iTJ1ik2Vr8Wk4CuW6z8eMIhEm4yHMDhSd5r2iX5IqlghpWvlsL47JQJUL 8Y43CA+ufoyEhyTNM88sJOmIAvFUCVV73Qc4sFpNjUUUlcsgVHNQeP+Lyt7T8a3NNzzK NUGQ/U5RlvFGlJVq/8+pdzdQAqFc2nrIdlLWg4YQfIOv5UTQ8+xPXm53ZY+RZkTLDNBi BDXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=paojrnHhCk6joQkSbRPWuj8brKBNVDh4fOd9v9QT3yk=; b=VRT+Adrt5j2kx7KLacvGmt5hefRq88U99k6uIBJzrxvslQlMU/Bq7uAqcJbZ9n8tX8 bxYx9nOwAkxLPAAxINNRk9YyNX/O7NxsL5cBrJ3zpdMU8bPk+Wv3WDMWZKwpty90yylh U0Nfw8eqhZNUHEB7n5ZhGnjqfSiv9JnzbJ3gN97WC5zCjo8uhO4vYlQ2DUMpRMZouF/N vpJA3O2siWHy2ByjmyHaSx4UQzhHLgGduO0bZHPuV6RVgRIzQsbG8MW2lRiZKoTCOT6e Guuoj1ZtqL4fShwmj66u0FY0YVbpmZlMGr7gSbp6W8/rYnUi9y6HMob8QGWMug5AiKpW s0HQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=r2keU11I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m3si12886038ilu.139.2021.10.30.03.07.43; Sat, 30 Oct 2021 03:07:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=r2keU11I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231830AbhJ3KFy (ORCPT + 99 others); Sat, 30 Oct 2021 06:05:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231792AbhJ3KFx (ORCPT ); Sat, 30 Oct 2021 06:05:53 -0400 Received: from mail-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36E46C061570 for ; Sat, 30 Oct 2021 03:03:23 -0700 (PDT) Received: by mail-io1-xd2f.google.com with SMTP id n11so2360037iod.9 for ; Sat, 30 Oct 2021 03:03:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=paojrnHhCk6joQkSbRPWuj8brKBNVDh4fOd9v9QT3yk=; b=r2keU11IaANOfySqmFsKeGHwR0AIOY4OIGLRQ5FFczqYA2aku5Jjs50BR9riOvlojW fvsZI0XApQ+mSc9xKn9KH/ya7nMwTYCPldyYq37qd7Rs27v6lOMNGOayyw0nxWTiEgKb icMBuHZNg4anOtmhLOsZ6Fn65N6BXBRBzR1bg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=paojrnHhCk6joQkSbRPWuj8brKBNVDh4fOd9v9QT3yk=; b=3ZmZGguGlMr3X76XkENP4y+Kf4XIXvtWI5/jzwSatevJonD8zrHcnElAU6i95bRw7q 1dl15uWZ5mT6jZkASMUfFSHKIpR+R530qmXbYYwP9kTu9tSp33CeDuoBE3ZcpeL3BOGa JXtB81TmCpJPW0zcOHVGJNX/NszEQkvZfOJg+j76YjZhpKda8mRVbpsJlEkDgDDcSlaH RIoZnzJL4uw6x4hLx9R29+Ek4smNaYfo66Nsu3mhc5EWcY6YtIY5mtrNGMRGRtns1YqC M7lqKuRArwb62IY6qPCQ3iuuzxi9taWjPDSYEOW9KzVYBH3ctxzjPlvkUkAnTiXXfpQl iiQg== X-Gm-Message-State: AOAM533udF5SESfXZbIH7UlGz0M3AHE4Y8INuVrzA4BAdCz6nDuo9kI+ VUOfPkch11EVomqSRIEHdtwX95Z2B3Aetg== X-Received: by 2002:a02:b1cd:: with SMTP id u13mr12089753jah.111.1635588201913; Sat, 30 Oct 2021 03:03:21 -0700 (PDT) Received: from ircssh-3.c.rugged-nimbus-611.internal (80.60.198.104.bc.googleusercontent.com. [104.198.60.80]) by smtp.gmail.com with ESMTPSA id s12sm228221iol.30.2021.10.30.03.03.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Oct 2021 03:03:20 -0700 (PDT) Date: Sat, 30 Oct 2021 10:03:19 +0000 From: Sargun Dhillon To: linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, willy@infradead.org, arnd@kernel.org, Willem de Bruijn Subject: epoll may leak events on dup Message-ID: <20211030100319.GA11526@ircssh-3.c.rugged-nimbus-611.internal> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I discovered an interesting behaviour in epoll today. If I register the same file twice, under two different file descriptor numbers, and then I close one of the two file descriptors, epoll "leaks" the first event. This is fine, because one would think I could just go ahead and remove the event, but alas, that isn't the case. Some example python code follows to show the issue at hand. I'm not sure if this is really considered a "bug" or just "interesting epoll behaviour", but in my opinion this is kind of a bug, especially because leaks may happen by accident -- especially if files are not immediately freed. I'm also not sure why epoll events are registered by file, and not just fd. Is the expectation that you can share a single epoll amongst multiple "users" and register different files that have the same file descriptor number (at least for purposes other than CRIU). Maybe someone can shed light on the behaviour. (FWIW: On closing of all references the file, the callback finally occurs clearing the event) #!/usr/bin/env python3 import select import socket import os n = 2 sockets = [] epoll = select.epoll() for i in range(n): sockets.append(socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)) sockets[-1].bind(('::1', 0)) for i in sockets: epoll.register(i) clones = [os.dup(i.fileno()) for i in sockets] epoll.poll() # Returns: [(4, 4), (5, 4)] for i in clones: epoll.register(i) epoll.poll() # Returns: [(4, 4), (5, 4), (6, 4), (7, 4)] socket0name = sockets[0].getsockname() socket0fileno = sockets[0].fileno() print(f'Closing {socket0fileno}') os.close(socket0fileno) epoll.poll() # Returns: [(4, 4), (5, 4), (6, 4), (7, 4)] badsocket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) badsocket.sendto(b'test', (socket0name[0], socket0name[1])) epoll.poll() # Returns [(4, 5), (5, 4), (6, 5), (7, 4)] # Indicating fd 4, and 6 have events. # Oh whoops, I forgot to unregister 4... epoll.unregister(4) # Returns: # Traceback (most recent call last): # File "", line 1, in # FileNotFoundError: [Errno 2] No such file or directory print(open(f'/proc/{os.getpid()}/fdinfo/{epoll.fileno()}').read()) # Returns: # pos: 0 # flags: 02000002 # mnt_id: 15 # ino: 10994 # tfd: 5 events: 1f data: 5 pos:0 ino:30ffc5 sdev:8 # tfd: 7 events: 1f data: 7 pos:0 ino:30ffc5 sdev:8 # tfd: 4 events: 1f data: 4 pos:0 ino:30ffc4 sdev:8 # tfd: 6 events: 1f data: 6 pos:0 ino:30ffc4 sdev:8