This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: list.extend() called on an iterator of the list itself leads to an infinite loop
Type: resource usage Stage:
Components: Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: aaron315, brian.curtin, giampaolo.rodola, neologix, petri.lehtinen, rhettinger, skrah
Priority: normal Keywords:

Created on 2012-03-20 04:37 by aaron315, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7)
msg156379 - (view) Author: aaron315 (aaron315) Date: 2012-03-20 04:37
alist=list(range(5))
alist.extend(enumerate(alist))
the computer will down !!!
msg156380 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2012-03-20 05:13
I just get a MemoryError. Do you actually receive a crash?
msg156382 - (view) Author: aaron315 (aaron315) Date: 2012-03-20 06:32
Have not been able to respond in a lower performance on the computer running, I restart the computer;
On another computer, indeed MemoryError。
msg156383 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-03-20 08:09
I can reproduce this with Python 3.2 on Linux-2.4.32/i686 with 512M of
RAM. The machine does not crash, it freezes completely in the same
manner as with a fork bomb. A hard reboot is required.
msg156384 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2012-03-20 08:32
This has the same effect:

a = list(range(5))
a.extend(iter(a))

So the problem is not in enumerate but in list.extend()
msg156385 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2012-03-20 08:43
I think you're simply running OOM, and Linux is thrashing to death.
If you wait long enough, the process should get nuked by the OOM killer (well, in theory).

What happens if you disable swap altogether ('swapoff -a')?
You can also change to strict overcommitting ('echo 2 > /proc/sys/vm/overcommit_memory').

Anyway, I guess you would get the same effect by simply running
list(range(<huge number>))...
msg156448 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-03-20 19:12
This a feature, not a bug.  Lists are allowed to mutate during iteration and there are valid use cases for doing so (adding pending tasks, etc.)  


# Demonstrate list mutation during iteration
# Infinite loop that overflows memory
s = [None]
for x in s:
   s.append(x)
History
Date User Action Args
2022-04-11 14:57:28adminsetgithub: 58578
2012-03-20 19:12:50rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg156448

resolution: not a bug
2012-03-20 11:38:02giampaolo.rodolasetnosy: + giampaolo.rodola
2012-03-20 08:43:54neologixsetnosy: + neologix
messages: + msg156385
2012-03-20 08:32:32petri.lehtinensetnosy: + petri.lehtinen

messages: + msg156384
title: enumerate() lead to system crashes -> list.extend() called on an iterator of the list itself leads to an infinite loop
2012-03-20 08:09:32skrahsettype: performance -> resource usage

messages: + msg156383
nosy: + skrah
2012-03-20 06:32:42aaron315setmessages: + msg156382
2012-03-20 05:13:05brian.curtinsetnosy: + brian.curtin
messages: + msg156380
2012-03-20 04:37:12aaron315create