gpt4 book ai didi

Python 脚本崩溃(munmap_chunk 无效指针)

转载 作者:行者123 更新时间:2023-12-05 04:36:31 30 4
gpt4 key购买 nike

我有一个用于气象站的长时间运行的 python 脚本。该脚本从天气传感器捕获数据并将它们上传到 mySql 数据库以及地下天气。

我遇到的问题是脚本将运行多天然后在没有回溯的情况下崩溃。我已经对此进行了数周的故障排除,但无济于事。我最初是在 IDLE 中运行脚本。我也在终端中运行了它。我“认为”的最新更新是真正的问题是我在 Visual Studio Code 中运行了脚本,当它崩溃时它发布了:munmap_chunk() invalid pointer in the terminal。

我的理解是 munmap_chunk 是一个 C 问题,所以它很可能是由我导入的模块生成的。我也明白,当一个指针传递给 free() 而不是通过 malloc() 获得时,就会抛出这个错误。

话虽如此,我不知道如何缩小实际产生问题的范围,以便我可以更正它。

在看到 munmap_chunk 错误之前,我以为可能是某个地方发生了内存泄漏。我手动调用了垃圾收集器,在几乎所有情况下你都不应该这样做。这允许脚本在最终再次崩溃之前运行更长时间。

我试图确定的主要事情是如何将问题的来源归零,因为脚本刚刚崩溃并且我没有得到任何回溯信息或抛出的异常。

  #!/usr/bin/python3

from gpiozero import Button
import time
import math
import Wind_Direction
import statistics
import Database
import requests
import sys
import logging
import board

from adafruit_bme280 import basic as adafruit_bme280

logging.basicConfig(format='%(asctime)s **%(levelname)4s** - %(module)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', filename='/home/testa/testb/testc.txt', filemode = 'w', level=logging.DEBUG)

logging.debug("The program has started")
print ("\nWeather_Station_BYO: The program has started")

i2c = board.I2C() # uses board.SCL and board.SDA

# ------- WU URL and credentials ---------

WUurl = "https://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?"
WUid = "TESTEST" # add your WS ID
WUpwd = "TESTEST" # add your password
WUcreds = "ID=" + WUid + "&PASSWORD="+ WUpwd

wind_count = 0 # Count how many half-rotations of the anemometer
radius_in = 3.54 # Radius of your anemometer in inches (9 cm)
wind_interval = 5 # How often (secs) to report speed
interval = 300 # 5*60 seconds = 5 minutes = 300 seconds
inches_in_mile = 63360
SECS_IN_AN_HOUR = 3600
ADJUSTMENT = 1.18 # Anemometer adjustment
BUCKET_SIZE = 0.011 # Rain gauge tips once when (.2794 mm = .011 in) of rain falls
rain_count = 0

store_speeds = []
store_directions = []

def spin():

"""Counts the number of every half-rotation of the anenmometer and returns that count"""

global wind_count

wind_count = wind_count + 1 # Every half-rotation, add 1 to count

# print("spin" + str(wind_count))

def calculate_speed(time_sec):

"""Uses the number of half-rotations stored in the global variable wind_count and the time used to count the rotations and generates a final wind speed"""

global wind_count

circumference_in = (2 * math.pi) * radius_in

rotations = wind_count / 2.0

dist_in = (circumference_in * rotations) / inches_in_mile # Calculate distance travelled by a cup in inches

mile_per_sec = dist_in / time_sec

mph = mile_per_sec * SECS_IN_AN_HOUR

final_speed = mph * ADJUSTMENT

# print("final speed" + str(final_speed))

return final_speed

def bucket_tipped():

"""Counts the number of times the rain gauge bucket filled and tipped"""

global rain_count

rain_count = rain_count + 1

# print(rain_count * BUCKET_SIZE)

def reset_rainfall():

"""Resets the global rain_count variable set in the bucket_tipped function"""

global rain_count

rain_count = 0

def reset_wind():

"""Resets the global wind_count variable set in the spin function"""

global wind_count

wind_count = 0

def read_all():

"""Reads bme280 (temp, humididty, pressure) values converts them to proper units and calculates the dewpoint"""

bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)

bme280.sea_level_pressure = 1013.25 # change this to match the location's pressure (hPa) at sea level

humidity = bme280.humidity # % Relative humidity

pressure = bme280.pressure * 100 / 3386 # Pascals converted to inHg

ambient_temperature = bme280.temperature * 9/5 + 32 # Convert to fahrenheit rounded

b = 17.62

c = 243.12

gamma = (b * bme280.temperature /(c + bme280.temperature)) + math.log(bme280.humidity / 100.0)

dewpoint = (c * gamma) / (b - gamma)

return humidity, pressure, ambient_temperature, dewpoint

wind_speed_sensor = Button(5)

wind_speed_sensor.when_pressed = spin # Whenever the wind sensor magnet closes the reed switch then add 1 to the number of rotations

# temp_probe = ds18b20_therm.DS18B20()

rain_sensor = Button(6)

rain_sensor.when_pressed = bucket_tipped

db = Database.weather_database()

i = 1
while db.wxlogin() != True:
time.sleep(30)
i = i + 1
if (i>5):
logging.debug("Could not establish a connection to the mysql database and program will be terminated.")
time.sleep(5)
sys.exit("Could not establish connection") # Exit the script after i amount of connection attempts

logging.debug("Initial connection to database was successful!")

print("\nWeather_Station_BYO: Initial connection to database was successful!")

# Loop to measure wind speed at 300-second intervals

while True:

try:
start_time = time.time()

while time.time() - start_time <= interval:

# print("In loop 1")

wind_start_time = time.time()

reset_wind()

while time.time() - wind_start_time <= wind_interval:

# print("In loop 2")

store_directions.append(Wind_Direction.get_value(wind_interval)) # wind_interval is how long to take measuremennts for wind direction

final_speed = calculate_speed(wind_interval) # Add this speed to the list & wind_interval is how long to take measurements for wind speed

store_speeds.append(final_speed)

logger = logging.getLogger(__name__)

logger.debug("Wind speed and direction obtained")

# print("Out of both loops")

# print("stored directions " + str(store_directions))

# print("stored speeds " + str(store_speeds))

wind_dir = Wind_Direction.get_average(store_directions)

wind_gust = max(store_speeds)

wind_speed = statistics.mean(store_speeds)

rainfall = rain_count * BUCKET_SIZE

reset_rainfall()

store_speeds = []

store_directions = []

humidity, pressure, ambient_temp, dewpoint = read_all()

logger.debug("Humidity, pressure, ambient_temp and dewpoint obtained")
# print(wind_dir)

if 348.75 <= wind_dir or wind_dir <= 11.25:
wind_direction = 'N'
elif 11.26 <= wind_dir <= 33.75:
wind_direction = 'NNE'
elif 33.76 <= wind_dir <= 56.25:
wind_direction = 'NE'
elif 56.26 <= wind_dir <= 78.75:
wind_direction = 'ENE'
elif 78.76 <= wind_dir <= 101.25:
wind_direction = 'E'
elif 101.25 <= wind_dir <= 123.75:
wind_direction = 'ESE'
elif 123.76 <= wind_dir <= 146.25:
wind_direction = 'SE'
elif 146.26 <= wind_dir <= 168.75:
wind_direction = 'SSE'
elif 168.76 <= wind_dir <= 191.25:
wind_direction = 'S'
elif 191.26 <= wind_dir <= 213.75:
wind_direction = 'SSW'
elif 213.76 <= wind_dir <= 236.25:
wind_direction = 'SW'
elif 226.26 <= wind_dir <= 258.75:
wind_direction = 'WSW'
elif 258.76 <= wind_dir <= 281.25:
wind_direction = 'W'
elif 281.26 <= wind_dir <= 303.75:
wind_direction = 'WNW'
elif 303.76 <= wind_dir <= 326.25:
wind_direction = 'NW'
elif 326.25 <= wind_dir <= 348.75:
wind_direction = 'NNW'
else: wind_direction = ''

#print('',wind_direction, 'Wind Direction \n',round(wind_speed,1), 'Wind Speed (mph) \n', round(wind_gust,1),'Wind Gust (mph) \n', round(rainfall,1), 'Rainfall (in) \n',
# round(humidity,1), '% Relative Humidity \n', round(dewpoint,1), 'Dew Point F \n', round(pressure,2), 'Barometric Pressure (inHg) \n', round(ambient_temp,1), 'Temperature F \n')

i = 1
while db.check_connection() != True:
time.sleep(30)
i = i + 1
if (i>5):
logger.debug("Connection to the mysql database was disconnected and connection could not be restablished. Program will be terminated.")
time.sleep(5)
sys.exit("Could not re-establish connection") # Exit the script after i amount of connection attempts
db.wxlogin()

db.insert(1,round(ambient_temp,1), 0, 0, round(pressure,2), round(humidity,1), round(dewpoint,1), wind_direction, round(wind_speed,1), round(wind_gust,1), round(rainfall,1)) # 10 items

# Weatherunderground
# print("Uploading to Weather Underground")

f_date = "&dateutc=now"
f_humid = "&humidity=" + str(humidity) # % Relative humidity
f_wspeed = "&windspeedmph=" + str(wind_speed) # mph
f_gust = "&windgustmph=" + str(wind_gust) # mph
f_airtemp = "&tempf=" + str(ambient_temp) # degrees F
f_rain = "&rainin=" + str(rainfall) # inches
f_press = "&baromin=" + str(pressure) # inches
# f_groundtemp = "&soiltempf=" + str(ground_temp_f) # degrees F
f_winddir = "&winddir=" + str(wind_dir) # degrees
f_action = "&action=updateraw"

try:

r = requests.get(WUurl+WUcreds+f_date+f_humid+f_wspeed+f_winddir+f_gust+f_airtemp+f_rain+f_press+f_action, timeout = 30)



print("\nReceived " + str(r.status_code) + " " + str(r.text) + " from WU")

except requests.ConnectionError as e:

logger.debug("HTTP Error - Connection Error. Make sure you are connected to the internet. Technical details given below.\n")

logger.debug(str(e))

print("\nHTTP Error - Connection Error. Make sure you are connected to internet. Technical details given below.\n")

print(str(e))

continue

except requests.Timeout as e:

logger.debug("HTTP Error - Timeout Error")

logger.debug(str(e))

print("\nHTTP Error - Timeout Error")

print(str(e))

continue

except requests.RequestException as e:

logger.debug("HTTP Error - General Error")

logger.debug(str(e))

print("\nHTTP Error - General Error")

print(str(e))

continue

except KeyboardInterrupt:

print("\nSomeone closed the program")

if (str(r.status_code) != '200'):
logger.debug("Upload to Weather Underground was not sucessful. HTTP Response Code: " + str(r.status_code) + " " + str(r.text))

except Exception:
logger.exception("Unhandled exception in the script!", exc_info = True)

VSCode 中故障处理程序的回溯:

munmap_chunk(): invalid pointer
Fatal Python error: Aborted

Thread 0xb0fa5440 (most recent call first):
File "/usr/lib/python3.7/threading.py", line 300 in wait
File "/usr/lib/python3.7/threading.py", line 552 in wait
File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 539 in held
File "/usr/lib/python3.7/threading.py", line 865 in run
File "/usr/lib/python3.7/threading.py", line 917 in _bootstrap_inner
File "/usr/lib/python3.7/threading.py", line 885 in _bootstrap
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydev_bundle/pydev_monkey.py", line 1054 in __call__

Thread 0xb27ff440 (most recent call first):
File "/usr/lib/python3.7/threading.py", line 300 in wait
File "/usr/lib/python3.7/threading.py", line 552 in wait
File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 543 in held
File "/usr/lib/python3.7/threading.py", line 865 in run
File "/usr/lib/python3.7/threading.py", line 917 in _bootstrap_inner
File "/usr/lib/python3.7/threading.py", line 885 in _bootstrap
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydev_bundle/pydev_monkey.py", line 1054 in __call__

Thread 0xb31ff440 (most recent call first):
File "/usr/lib/python3.7/threading.py", line 300 in wait
File "/usr/lib/python3.7/threading.py", line 552 in wait
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 246 in _on_run
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 46 in run
File "/usr/lib/python3.7/threading.py", line 917 in _bootstrap_inner
File "/usr/lib/python3.7/threading.py", line 885 in _bootstrap

Thread 0xb3bff440 (most recent call first):
File "/usr/lib/python3.7/threading.py", line 300 in wait
File "/usr/lib/python3.7/threading.py", line 552 in wait
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 200 in _on_run
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 46 in run
File "/usr/lib/python3.7/threading.py", line 917 in _bootstrap_inner
File "/usr/lib/python3.7/threading.py", line 885 in _bootstrap

Thread 0xb45ff440 (most recent call first):
File "/usr/lib/python3.7/threading.py", line 296 in wait
File "/usr/lib/python3.7/threading.py", line 552 in wait
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_timeout.py", line 43 in _on_run
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 46 in run
File "/usr/lib/python3.7/threading.py", line 917 in _bootstrap_inner
File "/usr/lib/python3.7/threading.py", line 885 in _bootstrap

Thread 0xb4fff440 (most recent call first):
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 219 in _read_line
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 237 in _on_run
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 46 in run
File "/usr/lib/python3.7/threading.py", line 917 in _bootstrap_inner
File "/usr/lib/python3.7/threading.py", line 885 in _bootstrap

Thread 0xb599b440 (most recent call first):
File "/usr/lib/python3.7/threading.py", line 300 in wait
File "/usr/lib/python3.7/queue.py", line 179 in get
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 382 in _on_run
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 46 in run
File "/usr/lib/python3.7/threading.py", line 917 in _bootstrap_inner
File "/usr/lib/python3.7/threading.py", line 885 in _bootstrap

Current thread 0xb6f34b40 (most recent call first):
File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 85 in _words_to_int
File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 220 in _read
File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 157 in value
File "/home/pi/Liberty Ridge WX Reduced/Wind_Direction.py", line 97 in get_value
File "/home/pi/Liberty Ridge WX Reduced/Liberty_Ridge_WX.py", line 199 in <module>
File "/usr/lib/python3.7/runpy.py", line 85 in _run_code
File "/usr/lib/python3.7/runpy.py", line 96 in _run_module_code
File "/usr/lib/python3.7/runpy.py", line 263 in run_path
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 285 in run_file
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 444 in main
File "/home/pi/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/__main__.py", line 45 in <module>
File "/usr/lib/python3.7/runpy.py", line 85 in _run_code
File "/usr/lib/python3.7/runpy.py", line 193 in _run_module_as_main

来自终端故障处理程序的回溯:

munmap_chunk(): invalid pointer
Fatal Python error: Aborted

Thread 0xb44ff440 (most recent call first):
File "/usr/lib/python3.7/threading.py", line 300 in wait
File "/usr/lib/python3.7/threading.py", line 552 in wait
File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 539 in held
File "/usr/lib/python3.7/threading.py", line 865 in run
File "/usr/lib/python3.7/threading.py", line 917 in _bootstrap_inner
File "/usr/lib/python3.7/threading.py", line 885 in _bootstrap

Thread 0xb4eff440 (most recent call first):
File "/usr/lib/python3.7/threading.py", line 300 in wait
File "/usr/lib/python3.7/threading.py", line 552 in wait
File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 539 in held
File "/usr/lib/python3.7/threading.py", line 865 in run
File "/usr/lib/python3.7/threading.py", line 917 in _bootstrap_inner
File "/usr/lib/python3.7/threading.py", line 885 in _bootstrap

Current thread 0xb6f53b40 (most recent call first):
File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 85 in _words_to_int
File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 220 in _read
File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 157 in value
File "/home/pi/Liberty Ridge WX Reduced/Wind_Direction.py", line 97 in get_value
File "/home/pi/Liberty Ridge WX Reduced/Liberty_Ridge_WX.py", line 189 in <module>
Aborted

最佳答案

我在我的 Raspberry Pi 气象站上运行非常相似的代码,并且在几天后从命令行或 Thonny 运行时出现 munmap_chunk() 错误。

像你一样,除了 munmap_chunk() 行之外,我没有得到任何信息。这让我认为错误不在 Python 中,因为它肯定会提供跟踪信息。所以,它可能来自 Python 使用的库,或者来自操作系统中进行内存管理的部分。

这是我认为是错误发生位置的源代码,注释很有趣...

static void
munmap_chunk (mchunkptr p)
{
INTERNAL_SIZE_T size = chunksize (p);

assert (chunk_is_mmapped (p));

/* Do nothing if the chunk is a faked mmapped chunk in the dumped
main arena. We never free this memory. */
if (DUMPED_MAIN_ARENA_CHUNK (p))
return;

uintptr_t block = (uintptr_t) p - prev_size (p);
size_t total_size = prev_size (p) + size;
/* Unfortunately we have to do the compilers job by hand here. Normally
we would test BLOCK and TOTAL-SIZE separately for compliance with the
page size. But gcc does not recognize the optimization possibility
(in the moment at least) so we combine the two values into one before
the bit test. */
if (__builtin_expect (((block | total_size) & (GLRO (dl_pagesize) - 1)) != 0, 0))
malloc_printerr ("munmap_chunk(): invalid pointer");

atomic_decrement (&mp_.n_mmaps);
atomic_add (&mp_.mmapped_mem, -total_size);

/* If munmap failed the process virtual memory address space is in a
bad shape. Just leave the block hanging around, the process will
terminate shortly anyway since not much can be done. */
__munmap ((char *) block, total_size);
}

关于Python 脚本崩溃(munmap_chunk 无效指针),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70823482/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com