Repository: streamlit/streamlit

Issue:
Possible memory leak: passing int(None) to st.slider step value will crash streamlit
# Summary

_Cells in the forum writes:_

> Can anybody else replicate this error I’m seeing?  Streamlit version 0.73.0 and 0.72.0

```
input_step = st.number_input("Step")
my_range = st.slider("Range", 20, 120, step=int(input_step))
```

The preceeding code results in my chrome returning an Out of Memory error code.

```Out of Memory
Out of Memory
743×280 4.83 KB
```

No console error is logged. However, if I change the st.number_input to provide a default value for the input_step, like so:

```
input_step = st.number_input("Step", value=5)
my_range = st.slider("Range", 20, 120, step=int(input_step))
```

This code produces the desired result. Digging into the slider.py file, it seems like st.slider should be able to handle a None value with the following code, line 192:

```
step = DEFAULTS[data_type]["step"]
```

Doing a little more digging, I found that

```
input_step = int(st.number_input("Step"))
my_range = st.slider("Range", 20, 120, step=input_step)
```

will also lead to a crash but only in the st.slider line. input_step = int(st.number_input("Step")) alone isn’t enough to crash streamlit, but passing the value into a subsequent widget is. So it seems that the mistake might be mine: that I can’t pass in int(None) as a step value to the st.slider, which makes sense as it’s a type error. Perhaps catching this as a TypeError would be a good thing to add?

Title of this post has been changed accordingly (from Possible memory leak: passing input of st.number_input without a default value to a subsequent st.slider will crash streamlit)

(also, is there a forum tag for bugs? I couldn’t find one in the dropdown menu here)

# Steps to reproduce

Code snippet:

```
input_step = st.number_input("Step")
my_range = st.slider("Range", 20, 120, step=int(input_step))
```

## Expected behavior:

st.slider displays as normal

## Actual behavior:

Streamlit crashes!

## Is this a regression?

probably not

# Debug info

- Streamlit version: 0.72 and 0.73
- Python version: (get it with `$ python --version`)
- Using Conda? PipEnv? PyEnv? Pex?
- OS version:
- Browser version:

# Additional information

Original forum discussion: https://discuss.streamlit.io/t/possible-memory-leak-passing-int-none-to-st-slider-step-value-will-crash-streamlit/8171



Discussion:

Resolving patch:
diff --git a/lib/streamlit/elements/slider.py b/lib/streamlit/elements/slider.py
--- a/lib/streamlit/elements/slider.py
+++ b/lib/streamlit/elements/slider.py
@@ -218,6 +218,11 @@ def all_same_type(items):
         if format is None:
             format = DEFAULTS[data_type]["format"]
 
+        if step == 0:
+            raise StreamlitAPIException(
+                "Slider components cannot be passed a `step` of 0."
+            )
+
         # Ensure that all arguments are of the same type.
         args = [min_value, max_value, step]
         int_args = all(map(lambda a: isinstance(a, int), args))


Test patch:
diff --git a/lib/tests/streamlit/slider_test.py b/lib/tests/streamlit/slider_test.py
--- a/lib/tests/streamlit/slider_test.py
+++ b/lib/tests/streamlit/slider_test.py
@@ -176,3 +176,10 @@ def test_value_out_of_bounds(self):
         self.assertEqual(
             "`min_value` (%s) must be >= -1.797e+308" % str(min_value), str(exc.value)
         )
+
+    def test_step_zero(self):
+        with pytest.raises(StreamlitAPIException) as exc:
+            st.slider("Label", min_value=0, max_value=10, step=0)
+        self.assertEqual(
+            "Slider components cannot be passed a `step` of 0.", str(exc.value)
+        )