# Buggy code
def getProcessList(self, sortedby="auto"):
    """
    Return the sorted process list
    """

    if not process_tag:
        return []
    if self.process == {} or "limits" not in globals():
        return self.process

    sortedReverse = True
    if sortedby == "auto":
        # Auto selection (default: sort by CPU%)
        sortedby = "cpu_percent"
        # Dynamic choice
        if "iowait" in self.cpu and self.cpu["iowait"] > limits.getCPUWarning(
            stat="iowait"
        ):
            # If CPU IOWait > 70% sort by IORATE usage
            sortedby = "io_counters"
        elif (
            self.mem["total"] != 0
            and self.mem["used"] * 100 / self.mem["total"] > limits.getMEMWarning()
        ):
            # If global MEM > 70% sort by MEM usage
            sortedby = "memory_percent"
    elif sortedby == "name":
        sortedReverse = False

    if sortedby == "io_counters":
        # Sort process by IO rate (sum IO read + IO write)
        listsorted = sorted(
            self.process,
            key=lambda process: process[sortedby][0]
            - process[sortedby][2]
            + process[sortedby][1]
            - process[sortedby][3],
            reverse=sortedReverse,
        )
    else:
        # Others sorts
        listsorted = sorted(
            self.process, key=lambda process: process[sortedby], reverse=sortedReverse
        )

    # Save the latest sort type in a global var
    self.process_list_sortedby = sortedby

    # Return the sorted list
    return listsorted


# Traceback (KeyError)
23.05.2013 11:52:54 Traceback (most recent call last):

File "/usr/share/glances/glances/glances.py", line 3696, in <module> main()

File "/usr/share/glances/glances/glances.py", line 3682, in main screen.update(stats)

File "/usr/share/glances/glances/glances.py", line 1760, in update self.flush(stats, cs_status=cs_status)

File "/usr/share/glances/glances/glances.py", line 1748, in flush self.display(stats, cs_status=cs_status)

File "/usr/share/glances/glances/glances.py", line 1712, in display processlist = stats.getProcessList(screen.getProcessSortedBy())

File "/usr/share/glances/glances/glances.py", line 1161, in getProcessList process[sortedby][3], reverse=sortedReverse)

File "/usr/share/glances/glances/glances.py", line 1160, in <lambda> process[sortedby][2] + process[sortedby][1] - KeyError: 'io_counters'

# Fixed code
def getProcessList(self, sortedby="auto"):
    """
    Return the sorted process list
    """

    if not process_tag:
        return []
    if self.process == {} or "limits" not in globals():
        return self.process

    sortedReverse = True
    if sortedby == "auto":
        # Auto selection (default: sort by CPU%)
        sortedby = "cpu_percent"
        # Dynamic choice
        if "iowait" in self.cpu and self.cpu["iowait"] > limits.getCPUWarning(
            stat="iowait"
        ):
            # If CPU IOWait > 70% sort by IORATE usage
            sortedby = "io_counters"
        elif (
            self.mem["total"] != 0
            and self.mem["used"] * 100 / self.mem["total"] > limits.getMEMWarning()
        ):
            # If global MEM > 70% sort by MEM usage
            sortedby = "memory_percent"
    elif sortedby == "name":
        sortedReverse = False

    if sortedby == "io_counters":
        try:
            # Sort process by IO rate (sum IO read + IO write)
            listsorted = sorted(
                self.process,
                key=lambda process: process[sortedby][0]
                - process[sortedby][2]
                + process[sortedby][1]
                - process[sortedby][3],
                reverse=sortedReverse,
            )
        except:
            listsorted = sorted(
                self.process,
                key=lambda process: process["cpu_percent"],
                reverse=sortedReverse,
            )
    else:
        # Others sorts
        listsorted = sorted(
            self.process, key=lambda process: process[sortedby], reverse=sortedReverse
        )

    # Save the latest sort type in a global var
    self.process_list_sortedby = sortedby

    # Return the sorted list
    return listsorted