Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s45o8-000Zrz-Ci for pgsql-hackers@arkaria.postgresql.org; Mon, 06 May 2024 21:23:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s45o5-00GCav-Lu for pgsql-hackers@arkaria.postgresql.org; Mon, 06 May 2024 21:23:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s45o5-00GCan-1v for pgsql-hackers@lists.postgresql.org; Mon, 06 May 2024 21:23:34 +0000 Received: from mail-yw1-x1130.google.com ([2607:f8b0:4864:20::1130]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s45o1-001txc-84 for pgsql-hackers@postgresql.org; Mon, 06 May 2024 21:23:33 +0000 Received: by mail-yw1-x1130.google.com with SMTP id 00721157ae682-61816fc256dso24859597b3.0 for ; Mon, 06 May 2024 14:23:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1715030607; x=1715635407; darn=postgresql.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=/sa6UKCeALRzOCvlmQJN7EleJg4P5BhzWvlBb0kAR80=; b=BZWWefqu7BfSgCCLGcT3f8bqw573/gCwE/k3TrnENOSj2gq/wLBEUcdqta8nMGBLgt +6hT/pgwNnUctMPHo8pW9PNqY32mu5iSHdikMazGba3HdtTlORGDcp08FaL6OOxpJu05 lmzh25GQ4GMc9Il8hmJKe+ga5BtkoNvpJlur4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715030607; x=1715635407; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/sa6UKCeALRzOCvlmQJN7EleJg4P5BhzWvlBb0kAR80=; b=XcyYfRS6rDro53bgCeIDJTa+8hbMSCF40DLMhuivERXjqYhuPwaMPg62PyON/uFTpw JeokPj0DU2vV0RiZBYpmJt5gW4I/wRhtNHpc/ApYACMGmYu01tNj88gKwP0F0kTj5Pz6 LrPxnGYJhcxXnAANP+iKUQU46qo5xxXJNQGqmLa8i8tnqMYdqGIWGWlq+7QWoK5lPlVD Flm/D4SiAB5yyXDwP+IN9CPA6FzoGqIbCLgIOHvJ92myYRmR4Dept33Dp2MoXsSZ7Hyr Sq+/sDzy+e3E6u9AoIvMKlReH07d5kqiXr4AB/ihguqUtxMQBe47OeXbUAK93PK8NBPu WPUw== X-Forwarded-Encrypted: i=1; AJvYcCVjAElHfsJ9z9mUbtA+a9qH9vG4sFPDlBCJRJ3uVmfK75UTmGK48VeRf+d7XbcYt+c8iXXo/Lh2xtknt6nFXLvyUbbdWYvR1kIVCc2J X-Gm-Message-State: AOJu0YzFcqQfjv/KjTARbD8D5Gx7s3dZ9aqwdOKiVmTxPVknALT67JNk kzZ0ZQJfHkO0WxkSBjF982v+wxdKhRaGEoPO/esPUO44bA/umDBfJlYs4hOrug== X-Google-Smtp-Source: AGHT+IE3UycAP0G/B0w75goLn45bhGyqYAz61MpvyW8IqkYDQL7yilWX71Tvk60PePltozkZjCZ37Q== X-Received: by 2002:a81:8443:0:b0:615:1e68:9080 with SMTP id u64-20020a818443000000b006151e689080mr10535907ywf.26.1715030607396; Mon, 06 May 2024 14:23:27 -0700 (PDT) Received: from google.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id x127-20020a817c85000000b00610d79fc429sm2366192ywc.78.2024.05.06.14.23.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 May 2024 14:23:26 -0700 (PDT) Date: Mon, 6 May 2024 14:23:24 -0700 From: Noah Misch To: Michael Paquier Cc: Heikki Linnakangas , Tom Lane , pgsql-hackers Subject: Re: Weird test mixup Message-ID: <20240506212324.2e.nmisch@google.com> References: <2387221.1710454778@sss.pgh.pa.us> <2393552.1710458033@sss.pgh.pa.us> <20240501231214.40@rfd.leadboat.com> <20240502193555.a1.nmisch@google.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="G0okDVnm3ADJuraz" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --G0okDVnm3ADJuraz Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, May 06, 2024 at 10:03:37AM +0900, Michael Paquier wrote: > On Thu, May 02, 2024 at 12:35:55PM -0700, Noah Misch wrote: > > I should have given a simpler example: > > > > s1: local-attach to POINT > > s2: enter InjectionPointRun(POINT), yield CPU just before injection_callback() > > s1: exit > > s2: wake up and run POINT as though it had been non-local Here's how I've patched it locally. It does avoid changing the backend-side, which has some attraction. Shall I just push this? > Hmm. Even if you were to emulate that in a controlled manner, you > would need a second injection point that does a wait in s2, which is > something that would happen before injection_callback() and before > scanning the local entry. This relies on the fact that we're holding > CPU in s2 between the backend shmem hash table lookup and the callback > being called. Right. We would need "second-level injection points" to write a test for that race in the injection point system. --G0okDVnm3ADJuraz Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="inplace030-inj-exit-race-v1.patch" Author: Noah Misch Commit: Noah Misch Fix race condition in backend exit after injection_points_set_local(). Symptoms were like those prompting commit f587338dec87d3c35b025e131c5977930ac69077. That is, under installcheck, backends from unrelated tests could run the injection points. Reviewed by FIXME. Discussion: https://postgr.es/m/FIXME diff --git a/src/test/modules/injection_points/injection_points.c b/src/test/modules/injection_points/injection_points.c index ace386d..75466d3 100644 --- a/src/test/modules/injection_points/injection_points.c +++ b/src/test/modules/injection_points/injection_points.c @@ -37,7 +37,13 @@ PG_MODULE_MAGIC; /* * Conditions related to injection points. This tracks in shared memory the - * runtime conditions under which an injection point is allowed to run. + * runtime conditions under which an injection point is allowed to run. Once + * set, a name is never changed. That avoids a race condition: + * + * s1: local-attach to POINT + * s2: yield CPU before InjectionPointRun(POINT) calls injection_callback() + * s1: exit() + * s2: run POINT as though it had been non-local * * If more types of runtime conditions need to be tracked, this structure * should be expanded. @@ -49,6 +55,9 @@ typedef struct InjectionPointCondition /* ID of the process where the injection point is allowed to run */ int pid; + + /* Should "pid" run this injection point, or not? */ + bool valid; } InjectionPointCondition; /* Shared state information for injection points. */ @@ -133,7 +142,7 @@ injection_point_allowed(const char *name) { InjectionPointCondition *condition = &inj_state->conditions[i]; - if (strcmp(condition->name, name) == 0) + if (condition->valid && strcmp(condition->name, name) == 0) { /* * Check if this injection point is allowed to run in this @@ -168,15 +177,16 @@ injection_points_cleanup(int code, Datum arg) { InjectionPointCondition *condition = &inj_state->conditions[i]; - if (condition->name[0] == '\0') + if (!condition->valid) continue; + Assert(condition->name[0] != '\0'); if (condition->pid != MyProcPid) continue; /* Detach the injection point and unregister condition */ InjectionPointDetach(condition->name); - condition->name[0] = '\0'; + condition->valid = false; condition->pid = 0; } SpinLockRelease(&inj_state->lock); @@ -299,11 +309,13 @@ injection_points_attach(PG_FUNCTION_ARGS) { InjectionPointCondition *condition = &inj_state->conditions[i]; - if (condition->name[0] == '\0') + if (strcmp(condition->name, name) == 0 || + condition->name[0] == '\0') { index = i; strlcpy(condition->name, name, INJ_NAME_MAXLEN); condition->pid = MyProcPid; + condition->valid = true; break; } } @@ -416,8 +428,8 @@ injection_points_detach(PG_FUNCTION_ARGS) if (strcmp(condition->name, name) == 0) { + condition->valid = false; condition->pid = 0; - condition->name[0] = '\0'; } } SpinLockRelease(&inj_state->lock); --G0okDVnm3ADJuraz--