:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(tasks): stop archived tasks blocking space deletion (#87004)
Thomas Obermüller67e9601148a6
diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.pyindex 0b1f76ef12d9..23ae43764b8c 100644--- a/products/tasks/backend/facade/api.py+++ b/products/tasks/backend/facade/api.py@@ -7231,7 +7231,7 @@ def update_channel(def delete_channel(channel_id: str | UUID, team_id: int, user_id: int | None) -> str:- """Soft-delete an empty public channel."""+ """Soft-delete an empty public channel. Archived tasks do not count as content."""channel = Channel.objects.filter(id=channel_id, team_id=team_id, deleted=False).first()if channel is None:return "not_found"@@ -7239,10 +7239,24 @@ def delete_channel(channel_id: str | UUID, team_id: int, user_id: int | None) ->return "personal" if channel.created_by_id == user_id else "not_found"if _is_general_channel(channel):return "general"- if channel.tasks.filter(deleted=False).exists() or channel.canvases.filter(deleted=False).exists():- return "not_empty"- channel.deleted = True- channel.save(update_fields=["deleted", "updated_at"])+ with transaction.atomic():+ # Emptiness is checked under a row lock because filing a task takes FOR KEY SHARE on+ # its channel: unlocked, a task can land after the check and be orphaned in a channel+ # this call goes on to delete.+ channel = Channel.objects.select_for_update().filter(id=channel_id, team_id=team_id, deleted=False).first()+ if channel is None:+ return "not_found"+ if (+ channel.tasks.filter(deleted=False, archived=False).exists()+ or channel.canvases.filter(deleted=False).exists()+ ):+ return "not_empty"+ # Not filtered on `archived`: flipping that flag leaves the FK untouched, so it takes+ # no lock on the channel and can happen after the check above. Task visibility joins+ # through the channel, so a task left pointing at a deleted one leaves every list.+ channel.tasks.filter(deleted=False).update(channel=None)+ channel.deleted = True+ channel.save(update_fields=["deleted", "updated_at"])return "ok"diff --git a/products/tasks/backend/tests/test_channels_api.py b/products/tasks/backend/tests/test_channels_api.pyindex 89b78ef46c42..20194d7ff7a0 100644--- a/products/tasks/backend/tests/test_channels_api.py+++ b/products/tasks/backend/tests/test_channels_api.py@@ -502,6 +502,22 @@ class ChannelsAPITestCase(TestCase):self.assertEqual(response.status_code, status.HTTP_409_CONFLICT)self.assertFalse(Channel.objects.unscoped().get(id=channel_id).deleted)+ def test_space_holding_only_archived_tasks_can_be_deleted(self):+ channel_id = self.client.post(self._channels_url(), {"name": "wrapped-up"}).json()["id"]+ task_id = self.client.post(+ self._tasks_url(),+ {"title": "Done with this", "description": "d", "channel": channel_id},+ ).json()["id"]+ self.client.patch(f"{self._tasks_url()}{task_id}/", {"archived": True}, format="json")++ response = self.client.delete(f"{self._channels_url()}{channel_id}/")++ self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, response.content)+ self.assertTrue(Channel.objects.unscoped().get(id=channel_id).deleted)+ self.assertIsNone(Task.objects.get(id=task_id).channel_id)+ listed = self.client.get(f"{self._tasks_url()}?archived=true").json()["results"]+ self.assertEqual([task["id"] for task in listed], [task_id])+def test_cannot_file_task_into_someone_elses_personal_channel(self):self._provision()personal = Channel.objects.unscoped().get(team=self.team, channel_type=Channel.ChannelType.PERSONAL)